Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add items to a layout in Qt?

I am developing an application in Qt 4.8.4, in which I do the following:

I subclass QGridLayout as follows:

class Viewer : public QGridLayout
{
    Q_OBJECT
    ...................
private:
    // Objects
    /// Maximize button object
    ViewerGeneric* viewerGeneric;

    /// Maximize button object
    QPushButton* btnMaximize;

    /// Close button object
    QPushButton* btnClose;

    /// Connect button object
    QPushButton* btnConnect;

    /// Central viewer layout object
    QGridLayout* viewer;

    /// Indicates the row position in the main grid
    unsigned int row;

    /// Indicates the column position in the main grid
    unsigned int col;
};

Then in the constructor I do something like this:

// Create the objects
btnMaximize = new QPushButton("max");
btnClose = new QPushButton("close");
btnConnect = new QPushButton("connect");

// Add the horizontal toolbar
QHBoxLayout* toolbar = new QHBoxLayout();
toolbar->setSizeConstraint(QLayout::SetMinimumSize);
toolbar->addItem(new QSpacerItem(0, 0, 
                     QSizePolicy::Expanding, QSizePolicy::Minimum));
toolbar->addWidget(btnMaximize);
toolbar->addWidget(btnClose);

// Add the 'Connect' button
viewer = new QGridLayout();
viewer->addWidget(btnConnect);

// Add the widgets
this->addItem(toolbar, 0, 0);
this->addItem(viewer, 0, 0, 2);

But, in the end, when I show the Viewer class in my main window the window is completely blank! Hope anybody can help me. Thank you.

Cheers,

like image 683
Didac Perez Parera Avatar asked Feb 18 '23 03:02

Didac Perez Parera


2 Answers

If parent widget is already visible, you need to call show() method of the later added widgets to make them visible.

like image 200
hyde Avatar answered Feb 27 '23 21:02

hyde


Finally I have found the solution:

For adding layouts into layouts, use addLayout() function instead of addItem(). I do not really know which is the difference but it works.

Thanks for your comments!

like image 41
Didac Perez Parera Avatar answered Feb 27 '23 20:02

Didac Perez Parera