[solved] Layout Management in a Groupbox



  • Hi all,

    I have an mainwindow appllication with 6 groupboxes. I would like to insert a group of lineedits (their corresponding labels), radibuttons and 2 pushbuttons. But in terms of layout management I have the following problems:

    a. Generally speaking, how can control the positions of the each widget in the groupbox? I can not locate any item to anywhere that I want. Which classes or functions are used to get the control?
    b. I try to control the layout with Qgridlayout and I try to locate each item(widget) with controlling row and cols. But i think it is not efficient.

    1. In my application, even I put the headers(Qlabel) in the 1st row of the gridlayout, there is a gap between them and 2nd row of the layouts. It should be closer to them.
    2. Even if I try to put the cancel button to the bottom right, cancel button is now on the top left.
    3. M label should be at the left side of the lineedit but it is at the right side of the okbutton.
      4} Also, except the buttons, I want to locate the labels and lineedits to more upper left but my app still alligns them to bottom. How can i get them upper and left?

    Thanks in advance,

    @//bcomp_main.cpp
    #include <QApplication>

    #include "bcomp_mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    bcomp_MainWindow Bcomp;

    Bcomp.showMaximized();
    return app.exec&#40;&#41;;
    

    }@

    @//TabModule.h
    #ifndef TABMODULE_H
    #define TABMODULE_H

    #include <QMainWindow>
    #include <QWidget>
    #include <QTabWidget>
    #include <QGroupBox>
    #include <QGridLayout>
    #include <QLabel>
    #include <QLineEdit>

    class TabModule {
    public:
    enum { NumTabs = 3};
    QTabWidget *tabWidget; //tab Widgets
    QWidget *tabs[NumTabs];
    QGridLayout *gridLayout;
    QGroupBox *groupbox[6];
    //QLabel *labels[i];
    };
    #endif // TABMODULE_H
    @

    @//bcomp_mainwindow.h
    #ifndef BCOMP_MAINWINDOW_H
    #define BCOMP_MAINWINDOW_H
    #include <QMainWindow>
    #include <QWidget>
    #include <QMenuBar>
    #include <QToolBar>
    #include <QStatusBar>
    #include "TabModule.h"

    class bcomp_MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    bcomp_MainWindow();

    private:
    // members
    TabModule *member;
    QWidget *centralWidget;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    //functions
    void mainSetup();

    signals:

    public slots:

    };

    #endif // BCOMP_MAINWINDOW_H
    @

    @//bcomp_mainwindow.cpp
    #include <QtGui>
    #include "bcomp_mainwindow.h"

    bcomp_MainWindow::bcomp_MainWindow( )
    {
    mainSetup(); // setup main items
    }

    void :: bcomp_MainWindow :: mainSetup ()
    {
    //Initial Size
    this -> resize(863, 423);
    this -> setMinimumSize(QSize(863, 423));
    this -> setMaximumSize(QSize(16777215, 16777215));
    //Tabs
    member = new TabModule();
    member -> tabWidget = new QTabWidget();
    for (int i = 0; i < member -> NumTabs; ++i) {
    member -> tabs[i] = new QWidget();
    member -> tabWidget->addTab(member -> tabs[i], QString(tr("Button %1").arg(i + 1)));}
    setCentralWidget(member -> tabWidget);

    //Groupboxes for tab1
    for (int i = 0; i <6 ; ++i){
        member -> groupbox[i] = new QGroupBox (member -> tabs[0]);
        member -> groupbox[i] -> setTitle(QString(tr("Grup %1").arg(i + 1)));}
    member -> gridLayout = new QGridLayout(member -> tabs[0]);
    member -> gridLayout -> setSpacing(20);
    member -> gridLayout -> addWidget(member -> groupbox[0] , 0, 0 ,1 ,2);
    member -> gridLayout -> addWidget(member -> groupbox[1] , 1, 0 ,9 ,2);
    member -> gridLayout -> addWidget(member -> groupbox[2] , 0, 2 ,7 ,3);
    member -> gridLayout -> addWidget(member -> groupbox[3] , 7, 2 ,3 ,3);
    member -> gridLayout -> addWidget(member -> groupbox[4] , 0, 5 ,5 ,5);
    member -> gridLayout -> addWidget(member -> groupbox[5] , 5, 5 ,5 ,5);
    member -> tabs[0] -> setLayout(member -> gridLayout);
    //Group of text edidors and labels in groupbox 3
    QString names [18] = {"Header1", "Header2","A","B","C","D","E","F","G","H","I","J","K","L","M","N","R","P"};
    QGridLayout *gLayout = new QGridLayout (member -> groupbox[2]);
    QLabel *labels[14];
    QLineEdit *inputbox[16];
    QRadioButton *radiobutton1 = new QRadioButton(names[16],member -> groupbox[2]);
    QRadioButton *radiobutton2 = new QRadioButton(names[17],member -> groupbox[2]);
    QPushButton *okbutton = new QPushButton(member -> groupbox[2]);
    QPushButton *canbutton = new QPushButton(member -> groupbox[2]);
    

    // QHBoxLayout *hLayout = new QHBoxLayout();

    for(int i = 0; i < 16; ++i){
        labels[i] = new QLabel(names[i],member -> groupbox[2]);
        if (i < 14)
            inputbox[i] = new QLineEdit(member -> groupbox[2]);
    }
    for(int i = 0; i < 26; ++i){
        if (i == 0) //Header 1 (1st row 1 st col)
            gLayout -> addWidget(labels[i], 0,0,1,5);
        else if (i == 1) //Header 2 (1st row 2nd col)
            gLayout -> addWidget(labels[i], 0,5,1,5);
        else if (i >= 2 && i < 8) //1st column
            gLayout -> addWidget(labels[i], i-1,0,1,1);
        else if (i >= 8 && i < 14) //2nd column
            gLayout -> addWidget(inputbox[i-8], i-7,1,1,4);
        else if (i >= 14 && i < 20) // 3rd col
            gLayout -> addWidget(inputbox[i-8], i-13,5,1,4);
        else if (i >= 20 && i < 26) //4 th coll
            gLayout -> addWidget(labels[i-12], i-19,9,1,1);
    }
    gLayout -> addWidget(radiobutton1, 7,0,1,2);
    gLayout -> addWidget(radiobutton2, 7,2,1,2);
    gLayout -> addWidget(labels[14], 8,0,1,1);
    gLayout -> addWidget(inputbox[12], 8,1,1,4);
    gLayout -> addWidget(inputbox[13], 8,5,1,4);
    gLayout -> addWidget(labels[15], 8,9,1,1);
    gLayout -> addWidget(okbutton, 9,10,1,1);
    gLayout -> addWidget(canbutton, 9,11,1,1);
    

    // hLayout -> addWidget(okbutton);
    // hLayout -> addWidget(canbutton);
    // gLayout -> addLayout(hLayout, 10,0,1,1);
    //gLayout -> setMargin(20);
    //gLayout -> setSpacing(10);
    member ->groupbox[2] -> setLayout(gLayout);
    }
    @


  • Lifetime Qt Champion

    Hi,

    Going through your code, I find it very hard to follow. I think you should rather create one widget derived e.g. from QGroupBox that contains the user interface you need and an interface to retrieve the data your. You can then re-use that widget as much as you need. It will make your code cleaner and easier to maintain.

    For the line edit part since each has a label, why not use a QFormLayout ? If your buttons are ok/cancel, you could use a QDialogButtonBox which will provide you with a ready made platform aware group of button. You can use multiple layouts to setup your widget.

    Hope it helps



  • Yes, it works well.

    I did as you said and it's done via using QFormLayout.

    Thanks,


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, can you please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

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