我正在尝试实现自定义小部件层次结构:
QMainWindow
-> QFrame
-> MyWidget
-> QFrame
->MySubWidget
这是 MyWidget 类的样子:
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *parent = 0, ...);
...
public slots:
void SlotFunction(int i);
...
private:
MySubWidget *sub_w;
QFrame *sub_frame;
...
}
如果我尝试在 MyWidget 构造函数期间创建 MySubWidget,则所有 MySubWidget 元素都会按预期显示:
MyWidget::MyWidget (...) : QWidget(parent) {
...
sub_frame = new QFrame(this);
...
sub_w = new MySubWidget(sub_frame); // commented out on a runtime test
}
但是,如果我尝试在运行时添加子小部件,则 sub_frame 保持空白。即信号反应:
void MyWidget::SlotFunction(int i) {
sub_w = new MySubWidget(sub_frame); // update, repaint, show and hide methods aren't helphul
}