Regarding Layout Management



  • Hello All

    I am trying to place below mentioned widgets on my main Window but they are not aligning properly PleaseHELP

    Widgets
    1.GroupBox
    2.In that GroupBox 2 PushButtons & One TableWidget

    Here is my code
    @QGroupBox *TabDialog::createFirstExclusiveGroup()
    {
    QGroupBox *groupBox = new QGroupBox(tr(""));

    QGridLayout *mainLayout = new QGridLayout;

    btnImport = new QPushButton(tr("&Import"));
    btnImport->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    connect(btnImport, SIGNAL(clicked()), this, SLOT(ImportCSVFile()));

    btnExport = new QPushButton(tr("&Export"));
    btnExport->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    connect(btnExport, SIGNAL(clicked()), this, SLOT(ExportCSVFile()));

    table = new QTableWidget(3,3);

    mainLayout->addWidget(btnImport, 0, 0 , 1, 2);
    mainLayout->addWidget(btnExport, 0, 1, 1, 2);
    mainLayout->addWidget(table, 0, 1, 1, 2);

    mainLayout->setColumnStretch(0,300);
    mainLayout->setColumnStretch(1, 500);

    groupBox->setLayout(mainLayout);

    return groupBox;
    }@



  • The order you use the calls does not set parents properly.
    Usually, I suggest to give the parent in the constructors:

    @
    QGridLayout *mainLayout = new QGridLayout(groupBox);
    btnImport = new QPushButton(tr("&Import"), groupBox);
    btnExport = new QPushButton(tr("&Export"), groupBox);
    table = new QTableWidget(3,3, groupBox);
    @

    The other possibility is to use the auto reparent feature of the layouts, but then, the parent of the layout must fit, which means the layout must have it's parent before addWidget is called.

    @
    groupBox->setLayout(mainLayout);
    mainLayout->addWidget(btnImport, 0, 0 , 1, 2);
    mainLayout->addWidget(btnExport, 0, 1, 1, 2);
    mainLayout->addWidget(table, 0, 1, 1, 2);
    @



  • I don't think that's the problem. I always construct layouts that way, and have never had any problems. With Qt 3.x you could run into all kinds of problems if you didn't do things in the right order, but with Qt 4.x it tends to just work, regardless of the order in which you create things and put them in the layouts.

    I think the problem is with the arguments to addWidget. Looks like you are adding btnExport and table in exactly the same place, for example. And btnImport and btnExport are overlapping.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.