所以,我把它放在了几个缺口上,并尝试使用简单的程序来获取Qt的工具。
首先,我在main()函数中尝试了一个简单的标签,然后在其中使用了一个按钮。一切都好。
接下来,我尝试了同样的方法,但在主窗口内(使用Qt创建的文档)。一键式程序工作后,我做了一个双按钮程序,这很简单。工作
然后,我尝试了Qt的Box Layouts。 在这些“主窗口”尝试中,我都没有更改由Qt。
创建的main.cpp文件这是mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPushButton *button1;
QPushButton *button2;
};
#endif // MAINWINDOW_H
接下来是mainwindow.cpp文件:
#include "mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("Two Button Example"));
resize(400,300);
button1 = new QPushButton(tr("Bye!"));
button1->setGeometry(0,0,200,30);
//button1->setParent(this);
connect(button1,SIGNAL(clicked()),this,SLOT(close()));
button1->show();
button2 = new QPushButton(tr("Hide B1!"));
button2->setGeometry(0,0,200,30);
//button2->setParent(this);
connect(button2,SIGNAL(clicked()),button2,SLOT(hide()));
button2->show();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
setLayout(layout);
}
MainWindow::~MainWindow()
{
}
我正在研究这本书(具体是布局,第2章,查找对话框示例(本书第16页,或pdf文件中的34):C++ GUI Programming with Qt 4 1st ed.pdf
对于这个特定问题,我也使用了这个:QHBoxLayout - Qt Examples and Tutorials 我注意到了:
显然,它们都遵循相同的语法和示例规则。那么,我做错了什么,或者不在这里做什么?如果使用“sizeHint()”函数来启发我也不是问题,那就太棒了。
提前致谢!
答案 0 :(得分:1)
我甚至不记得问题的根源,但我从来没有能够在窗口中使用布局。在我使用Qt的公司中,我们使用了一个中央小部件来管理布局,因此图表是:window - &gt;中央小部件 - &gt;布局 - &gt; subwidgets。如果我修改了你的代码,它将如下所示:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setWindowTitle(tr("Two Button Example"));
resize(400,300);
auto *parentWidget = new QWidget;
auto button1 = new QPushButton(tr("Bye!"), parentWidget);
button1->setGeometry(0,0,200,30);
connect(button1,SIGNAL(clicked()),this,SLOT(close()));
auto button2 = new QPushButton(tr("Hide B1!"), parentWidget);
button2->setGeometry(0,0,200,30);
connect(button2,SIGNAL(clicked()),button2,SLOT(hide()));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
parentWidget->setLayout(layout);
setCentralWidget(parentWidget);
}
此外,我不太明白为什么如果布局稍后重置它,你会尝试设置按钮大小。要更改布局内的大小,您必须更改小部件的sizeHint
:
setWindowTitle(tr("Two Button Example"));
resize(400,300);
auto *parentWidget = new QWidget;
auto button1 = new QPushButton(tr("Bye!"), parentWidget);
button1->setMaximumSize(20, 20);
connect(button1,SIGNAL(clicked()),this,SLOT(close()));
auto button2 = new QPushButton(tr("Hide B1!"), parentWidget);
button2->setMaximumSize(20, 20);
connect(button2,SIGNAL(clicked()),button2,SLOT(hide()));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
parentWidget->setLayout(layout);
setCentralWidget(parentWidget);
答案 1 :(得分:0)
QWidget centerWidget = new QWidget();
setCentralWidget(centerWidget);
//Add buttons, lists, and whatever
/*Add a QHBoxLayout or a QVBoxLayout and use
layout->addWidget() to add the widgets created,
like the examples. Then:*/
centerWidget->setLayout(layout);
它像Victor Polevoy建议的那样有效。只是回答,因为我相信我理解为什么不是我的QLayouts工作。