如何将项目添加到QT中的布局中

How can I add items to a layout in Qt?

本文关键字:QT 布局 添加 项目      更新时间:2023-10-16

我正在开发QT 4.8.4中的应用程序,其中我进行以下操作:

i子类qgridlayout如下:

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;
};

然后在构造函数中我做这样的事情:

// 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);

但是,最后,当我在主窗口中显示查看器类时,窗口为完全空白!希望任何人都能帮助我。谢谢。

欢呼,

如果父母已经可见,则需要调用后来添加的小部件的show()方法以使其可见。

最后我找到了解决方案:

对于将布局添加到布局中,请使用AddLayout()函数而不是AddItem()。我真的不知道哪个是区别,但有效。

感谢您的评论!

您可以使用Addwidget函数添加它...我尚未使用它...您可以尝试...

http://qt-project.org/doc/qt-4.8/qgridlayout.html

请参阅上一页以获取更多信息...

将它们布局放在父母上,然后使用addwidget函数向其添加小部件。

如果需要,可以使用show()和hide()功能避免闪烁并立即显示所有内容。

如果您不需要动态添加小部件,则应尝试使用QT设计器和创建.UI类