murzagurskiy
murzagurskiy

Reputation: 1323

QT. Add new layout to QLayout

I create interface dynamically when Application is run.

1) I have QTabWidget with 4 predefined tabs. But i must show only 1 or 2 tabs, in case of user shoice. On StackOverflow i learned, that i must keep all tabs in collection to add and destroit it.

I have QHash: twInputMethodsTabs = new QHash< int, QPair<QWidget*, QString> >();

First argument = index; Second = Tab Widget; Third = Tab Widget Caption Text;

2) I fill the collection like this:

for(int i = 0; i < ui->twInputMethods->children().length(); i++)
    {
        twInputMethodsTabs->insert(i, QPair<QWidget*, QString>(ui->twInputMethods->widget(i), ui->twInputMethods->tabText(i)));
    }

3) I add new widget in the tab like this:

twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);

4) How can i add new layout to this widget? I want to do like this:

QHBoxLayout *hblParams  =new QHBoxLayout();
twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);

But it does not work, because layout() returns QLayout which havent addLayout() function. How i can do this?

Or how can i should change architecture of code to do this?

Upvotes: 1

Views: 6906

Answers (3)

jstar
jstar

Reputation: 877

Cutted from "working" application:

  WidgetA::WidgetA(QWidget *parent) : QWidget(parent)
  {
      QVBoxLayout *pLayout = new QVBoxLayout();
      buildWidget(pLayout);
      this->setLayout(pLayout);
  }

void WidgetA::buildWidget(QVBoxLayout *layout){
  for(int i=0; i<4; ++i){
       this->buildSegments(layout);
  }
}

void WidgetA::buildSegments(QVBoxLayout *layout){
    QHBoxLayout *pHbl = new QHBoxLayout();
    QLabel *pSegmentSize = new QLabel(this);
    pSegmentSize->setText(tr("Segment Size(1,N)"));
    QSpinBox *pSegments = new QSpinBox(this);
    pHbl->addWidget(pSegmentSize);
    pHbl->addWidget(pSegments);
    layout->addItem(pHbl);
}   

Read this one: Widgets Tutorial - Nested Layouts

Upvotes: 0

mfitzp
mfitzp

Reputation: 15545

In this following code you get a widget (.first) and then select that widget's layout ->layout() and then add a Widget to that layout ->addWidget().

twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);

In the following code you get a widget (.first) and then select that widget's layout ->layout() and try to set the layout on the layout.

twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);

Replacing the QLayout

To set the layout on the parent widget, you need to drop the ->layout():

twInputMethodsTabs->value(1).first->addLayout(hblParams);

Note that since you are now adding an empty layout to the widget, any widgets current in the previous layout will be lost, so you may need to re-add the widgets to the layout.

Adding new QLayout inside existing QLayout

If you want to add a layout into the existing layout, you cannot do this directly. QLayout can only accept QWidget via .addWidget(). However, you can apply a layout to an empty QWidget() and then add that to the layout. For example:

QWidget *w = new QWidget();
w.addLayout(hblParams);
twInputMethodsTabs->value(1).first->layout()->addWidget(w);

An alternative is to set the layout on the QWidget to a layout that does support .addLayout() such as QHBoxLayout or QVBoxLayout. For example:

QVBoxLayout *l = new QVBoxLayout();
cmbbCommands.setLayout(l);  // Now has a layout that supports .addLayout
twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);

Now the following should work because ->layout() returns a QVBoxLayout:

QHBoxLayout *hblParams  =new QHBoxLayout();
twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);

Upvotes: 2

Paraboloid87
Paraboloid87

Reputation: 428

I Hope, I get what you want to do:

twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);

QHBoxLayout *hblParams  =new QHBoxLayout();
QWidget *w = new QWidget(twInputMethodsTabs->value(1).first);
twInputMethodsTabs->value(1).first->layout()->addWidget(w);

w->addLayout(hblParams);

I just wrote the code here, so it is untested. However it should give you an idea what I tried to explain in my comment.

Upvotes: 1

Related Questions