Adding widget into QGridLayout



  • I have problem. I creating widgets, layout and adding them like this:

    QGridLayout * layout = new QGridLayout();
        QLineEdit * firstNumberLabel = new QLineEdit("First number layout", this);
        QLineEdit * secondNumberLabel = new QLineEdit("Second number layout", this);
    
        layout->addWidget(firstNumberLabel, 0, 0);
        layout->addWidget(secondNumberLabel, 1, 0);
    
        this->setLayout(layout);
        this->setWindowTitle("IEEE Calculator");
        this->setFixedSize(1024, 768);
    

    But I can see only one, last created, widget. What is the problem? I added them correctly, propably...


  • Qt Champions 2016

    What does this refer to? What's the class you're doing this in?



  • QMainWindow subclass


  • Qt Champions 2016

    Set the layout to the central widget then. Something like this:

    QWidget * viewport = new QWidget(this);
    setCentralWidget(viewport);
    
    QGridLayout * layout = new QGridLayout(viewport);
    viewport->setLayout(layout);
    
    QLineEdit * firstNumberLabel = new QLineEdit("First number layout", this);
    QLineEdit * secondNumberLabel = new QLineEdit("Second number layout", this);
    
    layout->addWidget(firstNumberLabel, 0, 0);
    layout->addWidget(secondNumberLabel, 1, 0);
    
    setWindowTitle("IEEE Calculator");
    


  • Hi @Waxta

    You are trying to add layout to a widget (MainWindow ) which has already a layout,

    Actually according to Qt doc :

    http://doc.qt.io/qt-4.8/qmainwindow.html#qt-main-window-framework

    "QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below...."

    You need to have a central widget (as already suggested by @kshegunov ,and this centralWidget will contain your LineEdits).


Log in to reply
 

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