美文网首页
Qt MainWindow布局无法显示

Qt MainWindow布局无法显示

作者: Caiaolun | 来源:发表于2020-09-10 21:23 被阅读0次

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

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

相关文章

网友评论

      本文标题:Qt MainWindow布局无法显示

      本文链接:https://www.haomeiwen.com/subject/tmrmektx.html