为什么我的QBoxLayouts不起作用?

时间:2018-01-27 21:12:35

标签: c++ qt user-interface layout

所以,我把它放在了几个缺口上,并尝试使用简单的程序来获取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 我注意到了:

  • 评论QHBoxLayout部分,一直到“setLayout”函数,在程序中没有任何区别。我做过的测试没有受到这个QBoxLayout事情的影响;
  • “setGeometry”函数设置窗口小部件的位置(前两个参数)和大小(width和heightm,最后两个参数)。此位置与父窗口小部件相关,父窗口小部件可能是未分配父级时的屏幕本身;
  • 当按钮的“setParent”功能被注释并且“show”功能被取消注释时,按钮将显示在与MainWindow不同的窗口中。当我取消注释“setParent”功能时,按钮显示在MainWindow内部,如果按钮有“show”功能或没有“show”功能,则没有区别。详细信息是,对于我引用的书,到目前为止,没有任何示例需要声明父窗口小部件,Qt的示例和教程站点中的示例也没有;
  • 如果我不使用“setGeometry”功能,按钮很大,如600x600,无论如何。他们遵循上面关于其他功能的规则,但它们是巨大的,并且总是一个在另一个之上,而不是并排。书中广泛使用的函数“sizeHint()”也没有效果;

显然,它们都遵循相同的语法和示例规则。那么,我做错了什么,或者不在这里做什么?如果使用“sizeHint()”函数来启发我也不是问题,那就太棒了。

提前致谢!

2 个答案:

答案 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)

经过一番观察,我相信我知道它为什么不起作用。 在提供的示例中,该人正在使用QDialog或直接将布局应用于main()代码,而我正在使用QMainWindow类。 我想当你使用QMainWindow类时,你总是需要设置一个中央窗口小部件才能使布局工作(设置这个中央窗口小部件布局)。 所以解决方案是:

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工作。

相关问题