Problem with using layouts with custom widgets



  • I need to make a custom widget which will contain other custom widgets which are made up of qt QPushbuttons. All use layouts. The final widget does not build correctly, in that only one of the widgets in the final "big_wid" is visible.

    My code is hereunder and I think I am doing something conceptionally silly , but I just cannot see it.

    #include <QApplication>
    #include <QWidget>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QVBoxLayout>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QWidget* wid = new QWidget;
    QWidget* wid2 = new QWidget;
    QWidget* big_wid = new QWidget;
    
    //wid->setWindowTitle("My Widget");
    big_wid->setWindowTitle("My Big widget");
    
    QPushButton* pb1 = new QPushButton("PushButton_1");
    QPushButton* pb2 = new QPushButton("PushButton_2");
    QPushButton* pb3 = new QPushButton("PushButton_3");
    
    QHBoxLayout* layout = new QHBoxLayout;
    
    layout->addWidget(pb1);
    layout->addWidget(pb2);
    layout->addWidget(pb3);
    
    wid->setLayout(layout);
    wid2->setLayout(layout);
    
    QVBoxLayout* lay = new QVBoxLayout;
    
    lay->addWidget(wid);
    lay->addWidget(wid2);
    
    big_wid->setLayout(lay);
    
    big_wid->show();
    
    return a.exec();
    

    }

    All help gratefully received.


  • Qt Champions 2016

    @bigThrum
    Hi,

    wid->setLayout(layout);
    wid2->setLayout(layout);
    

    This will not work. You need to have a separate layout object for each of your widgets, and a separate set of QPushButton objects for each of the layouts.

    Kind regards.


Log in to reply
 

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