Qt MainWindow布局无法显示

2020-09-10  本文已影响0人  Caiaolun

为什么添加的控件显示不出或者堆成了一堆, 布局了也显示不出?

Answer:从QT Assistant 中找到这句话:

Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.

所以要先 new 一个widget然后调用:

void setCentralWidget( QWidget* widget)

例如:我们在mainwindow中添加2个控件:QLabel 和QLineEdit:

cenWidget = new QWidget(this); //this is point to QMainWindow

setCentralWidget(cenWidget);

QHBoxLayout* H = new QHBoxLayout(cenWidget);

label = new QLabel(cenWidget);

lineEdit = new QLineEdit(cenWidget);

H->addWidget(label);

H->addWidget(label);
上一篇 下一篇

猜你喜欢

热点阅读