سؤال

------------  ------
|          |  | 2  |
|          |  |    |
|     1    |  ------
|          |  ------  
|          |  |  3 |
------------  ------

How to arrange the QGridLayout like above?

I tried:

QGridLayout *layout = new QGridLayout();
centralWidget->setLayout (layout);

layout->addWidget (objOne, 0, 0);
layout->addWidget (objTwo, 0, 1);
layout->addWidget (objThree, 1, 1);

but failed.

هل كانت مفيدة؟

المحلول

Check the addWidget documentation. You can provide the rowSpan and columnSpan

QGridLayout *layout = new QGridLayout();
centralWidget->setLayout (layout);

layout->addWidget (objOne, 0, 0, -1, 1);
layout->addWidget (objTwo, 0, 1, 1, 1);
layout->addWidget (objThree, 1, 1, 1, 1);

Notice however that it is much easier to create the desired layout using QtDesigner. Check this Qt documentation page for more details

نصائح أخرى

Is it somehow mandatory for you to use QGridLayout for some reason? For simple layouts like this, I find it easier to use a combination of one QHBoxLayout and one QVBoxLayout.

QVBoxLayout* vlayout = new QVBoxLayout();
vlayout->addWidget(objTwo);
vlayout->addWidget(objThree);

QHBoxLayout* hlayout = new QHBoxLayout();
hlayout->addWidget(objOne);
hlayout->addLayout(vlayout);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top