【问题标题】:Layouts doesn't work in Qt布局在 Qt 中不起作用
【发布时间】:2012-05-22 18:47:39
【问题描述】:

我在 Qt 中遇到了布局问题。我正在尝试使用 2 个水平布局和一个垂直主布局来编译此代码。每个水平布局都有 3 个按钮,并且两个水平布局都包含在垂直布局中。但是在我编译完这段代码后,我只能看到一个只有“退出”按钮的小窗口。

firstline = new QHBoxLayout(this);
secondline = new QHBoxLayout(this);

layout = new QVBoxLayout(this);

eat = new QPushButton("Eat", this);
drink = new QPushButton("Drink", this);
smoke = new QPushButton("Smoke", this);

save = new QPushButton("Save", this);
load = new QPushButton("Load", this);
exit = new QPushButton("Exit", this);

firstline->addWidget(eat);
firstline->addWidget(drink);
firstline->addWidget(smoke);

secondline->addWidget(save);
secondline->addWidget(load);
secondline->addWidget(exit);

layout->addLayout(firstline);
layout->addLayout(secondline);

setLayout(layout);    

【问题讨论】:

  • 记住QGridLayout 在这种情况下可能更容易使用。
  • 我知道,但布局不起作用。我使用 QGridLayout 重写了这段代码,结果是一样的。问题一定是我的,或者我在代码中遗漏了一些东西。

标签: c++ qt layout button


【解决方案1】:

您已经通过这些语句设置对话框的布局...

 firstline = new QHBoxLayout(this);
 secondline = new QHBoxLayout(this);

所以调用它们的构造函数而不指定它们的父部件。

firstline = new QHBoxLayout();
secondline = new QHBoxLayout();

这将按照您的预期显示您的布局。

【讨论】:

  • @Mat 新调用中的括号有一个非常臭的区别:stackoverflow.com/a/620402/256138。最好只包括它们。
  • @rubenvb:我不同意。论文显然是非 POD,所有 Qt 文档都是在没有括号的情况下编写的。
  • 它仍然无法正常工作。让构造函数为空后的结果相同。
  • 确保您没有通过 Qt IDE 添加任何布局。它在我的系统上运行良好
  • 哦,我发现了问题并解决了。我使用 QMainWindow 作为窗口小部件,而不是使用 QWidget。谢谢你帮助我。
猜你喜欢
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
相关资源
最近更新 更多