c++ - 为什么 setCentralWidget 不起作用?

标签 c++ qt

这是我的类主窗口:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    menu* v = new menu(this);
    setCentralWidget(v);
}

还有我的菜单类:

menu::menu(MainWindow* parent){
    QLabel l = new QLabel("123");
    QVBoxLayout* lay = new QVBoxLayout;
    lay->addWidget(l);
    this->setLayout(lay);
    QWidget* a = new QWidget;
    QVBoxLayout* lay2 = new QVBoxLayout;
    QLabel* ll = new QLabel("456");
    lay2->addWidget(ll);
    a->setLayout(lay2);
    parent->setCentralWidget(a);
}

当我运行程序时,窗口显示 123,但我希望它显示 456。

setCentralWidget 方法是否不起作用?

最佳答案

setCentralWidget 工作正常。 您将小部件 menu 构造与其在外部小部件 (MainWindow) 中的位置混淆了。你应该把这些东西分开,否则你将无法,例如,在其他小部件中使用 menu

因此,您应该在构造函数中设置menu的外观,并且只在MainWindow中调用setCentralWidget

它应该是这样的:

文件.h

menu::menu(QWidget* parent = 0);

文件.cpp

menu::menu(QWidget* parent) : QWidget(parent)
{   
    // Create items
    QLabel* l = new QLabel("123", this);
    QLabel* ll = new QLabel("456", this);

    // Put items in layout
    QVBoxLayout* lay = new QVBoxLayout();
    lay->addWidget(l);
    lay->addWidget(ll);

    // Set "lay" as the layout of this widget
    setLayout(lay);
}

更新

因为想要的行为是有一个根据按钮点击切换 View 的界面:

enter image description here

最好的选择是使用 QStackedWidget

此处的示例代码将使用 QStackedWidget 生成此界面。

小部件1.h

#ifndef WIDGET1
#define WIDGET1

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

class Widget1 : public QWidget
{
    Q_OBJECT
public:
    Widget1(QWidget* parent = 0) : QWidget(parent) {
        QPushButton* btn = new QPushButton("Button Widget 1", this);
        QVBoxLayout* layout = new QVBoxLayout();
        layout->addWidget(btn);
        setLayout(layout);
        connect(btn, SIGNAL(clicked()), SIGNAL(buttonClicked()));
    }

signals:
    void buttonClicked();
};

#endif // WIDGET1

小部件2.h

#ifndef WIDGET2
#define WIDGET2

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

class Widget2 : public QWidget
{
    Q_OBJECT
public:
    Widget2(QWidget* parent = 0) : QWidget(parent) {
        QPushButton* btn1 = new QPushButton("Button 1 Widget 2", this);
        QPushButton* btn2 = new QPushButton("Button 2 Widget 2", this);
        QVBoxLayout* layout = new QVBoxLayout();
        layout->addWidget(btn1);
        layout->addWidget(btn2);
        setLayout(layout);
        connect(btn2, SIGNAL(clicked()), SIGNAL(button2Clicked()));
    }

signals:
    void button1Clicked();
    void button2Clicked();
};

#endif // WIDGET2

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void buttonWidget1Clicked();
    void button2Widget2Clicked();
private:
    QStackedWidget* m_sw;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include "widget1.h"
#include "widget2.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // Create Widgets
    Widget1* w1 = new Widget1(this);
    Widget2* w2 = new Widget2(this);
    QLabel* w3 = new QLabel("Result", this);

    m_sw = new QStackedWidget(this);
    m_sw->addWidget(w1);
    m_sw->addWidget(w2);
    m_sw->addWidget(w3);

    setCentralWidget(m_sw);

    connect(w1, SIGNAL(buttonClicked()), this, SLOT(buttonWidget1Clicked()));
    connect(w2, SIGNAL(button2Clicked()), this, SLOT(button2Widget2Clicked()));
}

void MainWindow::buttonWidget1Clicked()
{
    m_sw->setCurrentIndex(1); // Will show Widget2
}

void MainWindow::button2Widget2Clicked()
{
    m_sw->setCurrentIndex(2); // Will show Widgee3
}

MainWindow::~MainWindow() {}

关于c++ - 为什么 setCentralWidget 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31501462/

相关文章:

qt - 判断一个 QFont 是否支持某个 UTF-8 字符

Python QPushButton 设置图标 : put icon on button

c++ - 在 cmake 中使用两个工具链

c++ - 关联容器以多态方式处理一个元素

javascript - QML:Lambda 函数意外运行

linux - 库部署与未使用的直接依赖关系

C++ - 指针函数参数

c++ - QDesktopWidget 在屏幕分辨率更改或监视器计数时不发出任何信号

c++ - 这种类型的内存管理有用例吗?

c++ - QTCPSocket(Qt-C++)中逐字节传输音频文件