Qt 3 - 将小部件添加到现有小部件及其布局

时间:2013-12-17 13:32:30

标签: qt qwidget qtgui qt3

我在设计器中创建了一个QDialog QFrame testFrame,我已经添加了QHBoxLayout,并且在整个事物创建并显示之后的某个时刻我是尝试以编程方式创建一个以QFrame为父级的新窗口小部件。无论我尝试什么,新的小部件都不会显示:

init()方法结束:

QBoxLayout *testFrameLayout = new QHBoxLayout(this->testFrame); //QT3 docs use QBoxLayout pointer when creating QHBoxLayout

在活动期间(例如,按下了单独的按钮)

testButton = new QPushButton(this->testFrame);
testButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, false);
testButton->setMaximumWidth(50);
testButton->setMaximumHeight(50);
testButton->setMinimumWidth(50);
testButton->setMinimumHeight(50);

testFrameLayout->addWidget(testButton);

我使用不同的小部件,容器类(例如QGrid,我不需要管理任何布局内容),调用update()/ repaint()(在testFrame,testButton,对话框本身) )等等,无论我尝试在init()中完成它,但是如果在显示对话框后完成则无效。我一定错过了什么,所以任何帮助都会受到赞赏!

注意:我正在开发一个使用Qt 3的大型项目,因此解决方案必须与此版本兼容。

2 个答案:

答案 0 :(得分:0)

以下是我的Qt3应用程序中对话框中的一些工作代码:

QVBoxLayout *vb = new QVBoxLayout(this, 5, 5);
// ...
QHBoxLayout *askline = new QHBoxLayout(vb, 10);
QPushButton *ok      = new QPushButton(tr("OK"), this);
QPushButton *cancel  = new QPushButton(tr("Cancel"), this);
askline->addWidget(ok);
askline->addWidget(cancel);
adjustSize();
// ...

我认为您的问题是布局没有为QPushButton分配任何空间,并且需要adjustSize()

答案 1 :(得分:0)

调用

testButton->show();
最后解决了这个问题。由于在实际按钮上没有show()的情况下在框架上使用adjustSize()会使框架消失,我认为可能隐藏小部件,直到在这样添加时明确显示。