Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么';你不想工作吗?_C++_Qt - Fatal编程技术网

C++ 为什么';你不想工作吗?

C++ 为什么';你不想工作吗?,c++,qt,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

这是我的班级主窗口:

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
工作正常。 您正在将小部件
菜单
的构造与其在外部小部件(
MainWindow
)中的位置混淆。您应该将这些东西很好地分开,否则您将无法在其他小部件中使用
菜单

因此,您应该在构造函数中设置
菜单
的外观,并仅在
main窗口
中调用
setCentralWidget

它应该是这样的:

文件.h

menu::menu(QWidget* parent = 0);
file.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);
}

更新

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

最好的选择是使用

这里的示例代码将使用
QStackedWidget
生成此接口

widget1.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
\ifndef WIDGET1
#定义WIDGET1
#包括
#包括
#包括
类Widget1:公共QWidget
{
Q_对象
公众:
Widget1(QWidget*parent=0):QWidget(parent){
QPushButton*btn=新的QPushButton(“按钮小部件1”,本);
QVBoxLayout*布局=新的QVBoxLayout();
布局->添加小部件(btn);
设置布局(布局);
连接(btn,信号(点击()),信号(按钮点击());
}
信号:
void按钮单击();
};
#endif//WIDGET1
widget2.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
\ifndef WIDGET2
#定义WIDGET2
#包括
#包括
#包括
类Widget2:公共QWidget
{
Q_对象
公众:
Widget2(QWidget*parent=0):QWidget(parent){
QPushButton*btn1=新的QPushButton(“按钮1小部件2”,此);
QPushButton*btn2=新的QPushButton(“按钮2小部件2”,本);
QVBoxLayout*布局=新的QVBoxLayout();
布局->添加小部件(btn1);
布局->添加小部件(btn2);
设置布局(布局);
连接(btn2,信号(点击()),信号(按钮2点击());
}
信号:
无效按钮1();
void按钮();
};
#endif//WIDGET2
主窗口

#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
\ifndef主窗口
#定义主窗口
#包括
#包括
类主窗口:公共QMainWindow
{
Q_对象
公众:
主窗口(QWidget*父窗口=0);
~main窗口();
公众时段:
void按钮widget1clicked();
void按钮2widget2clicked();
私人:
QStackedWidget*m_sw;
};
#endif//main窗口
mainwindow.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() {}
#包括“mainwindow.h”
#包括
#包括
#包括“widget1.h”
#包括“widget2.h”
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
//创建小部件
Widget1*w1=新Widget1(本);
Widget2*w2=新的Widget2(本);
QLabel*w3=新的QLabel(“结果”,this);
m_sw=新的QStackedWidget(本);
m_sw->addWidget(w1);
m_sw->addWidget(w2);
m_sw->addWidget(w3);
setCentralWidget(m_西南);
连接(w1,信号(buttonClicked()),此,插槽(buttonWidget1Clicked());
连接(w2,信号(button2Clicked()),此,插槽(button2Widget2Clicked());
}
void主窗口::按钮Widget1Clicked()
{
m_sw->setCurrentIndex(1);//将显示Widget2
}
void主窗口::button2Widget2Clicked()
{
m_sw->setCurrentIndex(2);//将显示Widgee3
}
MainWindow::~MainWindow(){}

我想测试它是否可以在另一个类中调用setCentralWidget。所以我永远不能在另一个类中调用setCentralWidget?我只想找到一个方法来切换QWidget。如何实现切换窗口?我想知道>这不是一个好的设计。解释你想做什么,我们会找到更好的方法