如何将项目添加到QT中的布局中
How can I add items to a layout in Qt?
我正在开发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类
相关文章:
- Qt自定义QPush按钮未显示在布局上
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- 如何在qt中将对象添加到现有布局中?--已解决
- 集合布局上的 Qt 分割错误
- QT将图像从缩略图拖放到网格布局(QVTKOpenGLWidget)?
- Qt小部件将子布局堆叠在彼此之上
- 更改 QT 布局背景
- Qt:自定义小部件的多个布局
- Mac上的QT:形式布局无法水平伸展
- QT QML ListView布局未调整大小
- QT设计器中的自定义小部件在框架中具有预先存在的布局
- Qt 删除嵌套布局
- C ,QT,防止窗口大小大于实际布局高度
- 重叠小部件的 QT 自定义布局示例无法编译
- Qt 布局不起作用
- QT创建者 - 如何启用浮动IDE布局
- QT布局小部件调整大小
- QLayoutItem和QWidget之间的Qt布局关系
- 清理Qt布局并添加其他小部件不起作用.幽灵小部件留下来.Qt错误
- 在QMain窗口上添加QT布局的代码