Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, October 2007
How to retrieve a layout?


Message 1 in thread

All,

After adding a widget to a layout, the layout manager take care its 
position.
My question is: is there a method to retrieve the layout?
For example,

    QPushButton *btn = new QPushButton;
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(btn);

    QLayout *layout = btn->layout();
    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);

I wish p is vbox, but it is not.
Would you like teach me how to retrieve the layout?

Thanks,
Lingfa


--
 [ signature omitted ] 

Message 2 in thread

Calling "layout()" returns the layout of a (container-) widget. That layout has usually been assigned to the widget via "setLayout(..)", before, and it will take care of the child widgets' dimensions and positions.

In Your example, the "btn" is itself layed out by the "vbox" layout. And the "btn" does not have a layout for its child widgets. So it returns "0"

From "QLayout *QWidget::layout() const" docs:
   "Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed."

Your code could work like this:
    QWidget *outerWidget = new QWidget;          // added line
    QPushButton *btn = new QPushButton;
    QVBoxLayout *vbox = new QVBoxLayout;
    outerWidget->setLayout(vbox);                // added line
    vbox->addWidget(btn);
 
    QLayout *layout = outerWidget->layout();     // changed line
    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);

Alessandro

Lingfa Yang schrieb:
> All,
> 
> After adding a widget to a layout, the layout manager take care its 
> position.
> My question is: is there a method to retrieve the layout?
> For example,
> 
>    QPushButton *btn = new QPushButton;
>    QVBoxLayout *vbox = new QVBoxLayout;
>    vbox->addWidget(btn);
> 
>    QLayout *layout = btn->layout();
>    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);
> 
> I wish p is vbox, but it is not.
> Would you like teach me how to retrieve the layout?

--
 [ signature omitted ] 

Message 3 in thread

Alessandro,

I get your point, but actualy I have no outerWidget.
I have a vbox which holds a button, then, I have hbox holds many vboxes.
    QVBoxLayout *vbox0 = new QVBoxLayout;
    QVBoxLayout *vbox1 = new QVBoxLayout;
    QVBoxLayout *vbox2 = new QVBoxLayout;

    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addLayout(vbox0);
    hbox->addLayout(vbox1);
    hbox->addLayout(vbox2);

One of the vbox has my target button.
   QPushButton *btn = new QPushButton;
   vbox1->addWidget(btn);

I am looking a way to find which vbox has the target button.
If there is no way to ask the button who hold him (inner->outer), is 
there a (outer->inner) way to search hbox, and retrieve vboxes and their 
belong widgets to match the target button?

Thanks,
Lingfa

> ample, the "btn" is itself layed out by the "vbox" layout. And the 
> "btn" does not have a layout for its child widgets. So it returns "0"
>
>> From "QLayout *QWidget::layout() const" docs:
>
>   "Returns the layout manager that is installed on this widget, or 0 
> if no layout manager is installed."
>
> Your code could work like this:
>    QWidget *outerWidget = new QWidget;          // added line
>    QPushButton *btn = new QPushButton;
>    QVBoxLayout *vbox = new QVBoxLayout;
>    outerWidget->setLayout(vbox);                // added line
>    vbox->addWidget(btn);
>
>    QLayout *layout = outerWidget->layout();     // changed line
>    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);
>
> Alessandro
>
> Lingfa Yang schrieb:
>
>> All,
>>
>> After adding a widget to a layout, the layout manager take care its 
>> position.
>> My question is: is there a method to retrieve the layout?
>> For example,
>>
>>    QPushButton *btn = new QPushButton;
>>    QVBoxLayout *vbox = new QVBoxLayout;
>>    vbox->addWidget(btn);
>>
>>    QLayout *layout = btn->layout();
>>    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);
>>
>> I wish p is vbox, but it is not.
>> Would you like teach me how to retrieve the layout?
>
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>


--
 [ signature omitted ] 

Message 4 in thread

Lingfa,

I see, it is indeed more advanced than I thought. There is no direct innerWidget->outerLayout access (AFAIK). But outerLayout->innerWidget should work with a few FORs and IFs.

"QLayoutItem *QLayout::itemAt(int index)", "int QLayout::count()", "int QLayout::indexOf(QWidget *widget)", and friends should help you traverse the layout structure outer->inner until You found your widget.
I would start with the "*btn"s' parents' layout and search from there.

Alessandro

Lingfa Yang schrieb:
> Alessandro,
> 
> I get your point, but actualy I have no outerWidget.
> I have a vbox which holds a button, then, I have hbox holds many vboxes.
>    QVBoxLayout *vbox0 = new QVBoxLayout;
>    QVBoxLayout *vbox1 = new QVBoxLayout;
>    QVBoxLayout *vbox2 = new QVBoxLayout;
> 
>    QHBoxLayout *hbox = new QHBoxLayout;
>    hbox->addLayout(vbox0);
>    hbox->addLayout(vbox1);
>    hbox->addLayout(vbox2);
> 
> One of the vbox has my target button.
>   QPushButton *btn = new QPushButton;
>   vbox1->addWidget(btn);
> 
> I am looking a way to find which vbox has the target button.
> If there is no way to ask the button who hold him (inner->outer), is 
> there a (outer->inner) way to search hbox, and retrieve vboxes and their 
> belong widgets to match the target button?
> 
> Thanks,
> Lingfa
> 
>> ample, the "btn" is itself layed out by the "vbox" layout. And the 
>> "btn" does not have a layout for its child widgets. So it returns "0"
>>
>>> From "QLayout *QWidget::layout() const" docs:
>>
>>   "Returns the layout manager that is installed on this widget, or 0 
>> if no layout manager is installed."
>>
>> Your code could work like this:
>>    QWidget *outerWidget = new QWidget;          // added line
>>    QPushButton *btn = new QPushButton;
>>    QVBoxLayout *vbox = new QVBoxLayout;
>>    outerWidget->setLayout(vbox);                // added line
>>    vbox->addWidget(btn);
>>
>>    QLayout *layout = outerWidget->layout();     // changed line
>>    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);
>>
>> Alessandro
>>
>> Lingfa Yang schrieb:
>>
>>> All,
>>>
>>> After adding a widget to a layout, the layout manager take care its 
>>> position.
>>> My question is: is there a method to retrieve the layout?
>>> For example,
>>>
>>>    QPushButton *btn = new QPushButton;
>>>    QVBoxLayout *vbox = new QVBoxLayout;
>>>    vbox->addWidget(btn);
>>>
>>>    QLayout *layout = btn->layout();
>>>    QVBoxLayout *p = dynamic_cast<QVBoxLayout *> (layout);
>>>
>>> I wish p is vbox, but it is not.
>>> Would you like teach me how to retrieve the layout?
>>
>>
>> -- 
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>> "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
> 
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ] 

Message 5 in thread

Alessandro Portale wrote:

> Lingfa,
>
> I see, it is indeed more advanced than I thought. There is no direct 
> innerWidget->outerLayout access (AFAIK). But outerLayout->innerWidget 
> should work with a few FORs and IFs.
>
> "QLayoutItem *QLayout::itemAt(int index)", "int QLayout::count()", 
> "int QLayout::indexOf(QWidget *widget)", and friends should help you 
> traverse the layout structure outer->inner until You found your widget.
> I would start with the "*btn"s' parents' layout and search from there.
>
> Alessandro

Alessandro,

Thank you for your nice help. If no easy solution I will dig dig and dig :-)

An alternative way could be let the button memorize who hold it.
    QPushButton *btn = new QPushButton("First");
    vbox1->addWidget(btn);  // add a btn into the layout
    btn->setProperty("outer", qint64(vbox1)); // let the btn know who

Later, retrieve the layout and keep adding other widgets:
    QVBoxLayout *p = (QVBoxLayout *)(btn->property ("outer").toInt());
    p->addWidget(new QPushButton("Second")); // keep adding

This solution looks simpler, but I doubt if it is safe or not, because 
there is no object and no dynamic casting, just an interger.

Regards,
Lingfa


--
 [ signature omitted ]