C++Qt不用Qt工程师,用QStackedWidget完整代码实现在主窗口中用点击菜单进行三个子窗口之间进行切换,并且保证各个窗口之间不会产生冲突
时间: 2023-06-24 22:03:08 浏览: 96
以下是一个使用 QStackedWidget 实现在主窗口中切换子窗口的示例代码:
mainwindow.h:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStackedWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_menu1_triggered();
void on_menu2_triggered();
void on_menu3_triggered();
private:
QStackedWidget *stackedWidget;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 创建菜单栏
QMenuBar *menuBar = new QMenuBar(this);
setMenuBar(menuBar);
// 创建菜单
QMenu *menu1 = new QMenu("窗口1", this);
QMenu *menu2 = new QMenu("窗口2", this);
QMenu *menu3 = new QMenu("窗口3", this);
menuBar->addMenu(menu1);
menuBar->addMenu(menu2);
menuBar->addMenu(menu3);
// 创建菜单项
QAction *action1_1 = new QAction("显示窗口1", this);
QAction *action1_2 = new QAction("显示窗口2", this);
QAction *action1_3 = new QAction("显示窗口3", this);
QAction *action2_1 = new QAction("显示窗口1", this);
QAction *action2_2 = new QAction("显示窗口2", this);
QAction *action2_3 = new QAction("显示窗口3", this);
QAction *action3_1 = new QAction("显示窗口1", this);
QAction *action3_2 = new QAction("显示窗口2", this);
QAction *action3_3 = new QAction("显示窗口3", this);
menu1->addAction(action1_1);
menu1->addAction(action1_2);
menu1->addAction(action1_3);
menu2->addAction(action2_1);
menu2->addAction(action2_2);
menu2->addAction(action2_3);
menu3->addAction(action3_1);
menu3->addAction(action3_2);
menu3->addAction(action3_3);
// 连接菜单项的点击事件
connect(action1_1, &QAction::triggered, this, &MainWindow::on_menu1_triggered);
connect(action1_2, &QAction::triggered, this, &MainWindow::on_menu2_triggered);
connect(action1_3, &QAction::triggered, this, &MainWindow::on_menu3_triggered);
connect(action2_1, &QAction::triggered, this, &MainWindow::on_menu1_triggered);
connect(action2_2, &QAction::triggered, this, &MainWindow::on_menu2_triggered);
connect(action2_3, &QAction::triggered, this, &MainWindow::on_menu3_triggered);
connect(action3_1, &QAction::triggered, this, &MainWindow::on_menu1_triggered);
connect(action3_2, &QAction::triggered, this, &MainWindow::on_menu2_triggered);
connect(action3_3, &QAction::triggered, this, &MainWindow::on_menu3_triggered);
// 创建 QStackedWidget
stackedWidget = new QStackedWidget(this);
setCentralWidget(stackedWidget);
// 创建三个子窗口
QWidget *widget1 = new QWidget(this);
QHBoxLayout *layout1 = new QHBoxLayout(widget1);
layout1->addWidget(new QLabel("窗口1", widget1));
widget1->setLayout(layout1);
stackedWidget->addWidget(widget1);
QWidget *widget2 = new QWidget(this);
QHBoxLayout *layout2 = new QHBoxLayout(widget2);
layout2->addWidget(new QLabel("窗口2", widget2));
widget2->setLayout(layout2);
stackedWidget->addWidget(widget2);
QWidget *widget3 = new QWidget(this);
QHBoxLayout *layout3 = new QHBoxLayout(widget3);
layout3->addWidget(new QLabel("窗口3", widget3));
widget3->setLayout(layout3);
stackedWidget->addWidget(widget3);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_menu1_triggered()
{
stackedWidget->setCurrentIndex(0);
}
void MainWindow::on_menu2_triggered()
{
stackedWidget->setCurrentIndex(1);
}
void MainWindow::on_menu3_triggered()
{
stackedWidget->setCurrentIndex(2);
}
```
在这个示例中,我们首先创建了一个菜单栏和三个菜单,每个菜单下面有三个菜单项。然后我们创建了一个 QStackedWidget,并将其设置为主窗口的中央部件。接下来,我们创建了三个 QWidget 子窗口,并将它们添加到 QStackedWidget 中。最后,我们连接了菜单项的点击事件,并在点击时设置 QStackedWidget 的当前索引以切换窗口。
阅读全文