gpt4 book ai didi

c++ - 布局在 Visual Studio 2015 中不起作用?

转载 作者:行者123 更新时间:2023-11-30 05:10:46 25 4
gpt4 key购买 nike

我想在 qt5 中使用布局,但在 Visual Studio 2015 中布局不起作用?

这是我的代码:

layout.h代码

#ifndef LAYOUT_H
#define LAYOUT_H

#include <QtWidgets/QMainWindow>
#include "ui_layout.h"

class layout : public QMainWindow
{
Q_OBJECT

public:
layout(QWidget *parent = 0);
~layout();

private:
Ui::layoutClass ui;
};

#endif // LAYOUT_H

main.cpp

#include "layout.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
layout w;
QHBoxLayout hb;
QPushButton b("button 0");
QPushButton b1("button 1");

hb.addWidget(&b);
hb.addWidget(&b1);

w.setLayout(&hb);
w.show();
return a.exec();
}

这是我的结果: enter image description here

如何解决这个问题?

最佳答案

QMainWindow是一个特殊的小部件,因为它具有默认小部件,如 QStatusbar、QMenuBar 等。使用此小部件时,我们必须将新元素放置在 centralWidget 中。

enter image description here

所以我们必须分配一个 widget 作为我们的 centralWidget,然后我们添加如下所示的布局:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
layout w;
w.setCentralWidget(new QWidget);


QHBoxLayout hb;
QPushButton b("button 0");
QPushButton b1("button 1");

hb.addWidget(&b);
hb.addWidget(&b1);

w.centralWidget()->setLayout(&hb);
w.show();

return a.exec();
}

关于c++ - 布局在 Visual Studio 2015 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45509436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com