C++Qt不用QT设计师实现,点击主窗口的Toolbutton,弹出子窗口,并用QStackedWidget完整代码实现在子窗口中点击第一个菜单,切换到第一子窗口,点击第二个菜单,切换到第二个子窗口,点击第三个菜单,切换到第三个子窗口,并且保证各个窗口之间不会产生冲突
时间: 2023-06-19 21:09:01 浏览: 127
以下是一个简单的实现,其中使用了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_toolButton_clicked();
void on_menu1_triggered();
void on_menu2_triggered();
void on_menu3_triggered();
private:
QStackedWidget *stackedWidget;
QWidget *widget1, *widget2, *widget3;
};
#endif // MAINWINDOW_H
```
Mainwindow.cpp文件:
```cpp
#include "mainwindow.h"
#include <QToolButton>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 创建工具栏和工具按钮
QToolBar *toolBar = addToolBar(tr("Tool Bar"));
QToolButton *toolButton = new QToolButton(this);
toolButton->setText(tr("Open Sub Window"));
toolButton->setMenu(new QMenu(this));
toolBar->addWidget(toolButton);
// 创建菜单和菜单项
QMenuBar *menuBar = new QMenuBar(this);
QMenu *menu = new QMenu(tr("Sub Window"), this);
QAction *menu1 = new QAction(tr("Sub Window 1"), this);
QAction *menu2 = new QAction(tr("Sub Window 2"), this);
QAction *menu3 = new QAction(tr("Sub Window 3"), this);
menu->addAction(menu1);
menu->addAction(menu2);
menu->addAction(menu3);
menuBar->addMenu(menu);
setMenuBar(menuBar);
// 创建子窗口并添加到QStackedWidget控件中
stackedWidget = new QStackedWidget(this);
widget1 = new QWidget(this);
widget2 = new QWidget(this);
widget3 = new QWidget(this);
stackedWidget->addWidget(widget1);
stackedWidget->addWidget(widget2);
stackedWidget->addWidget(widget3);
// 设置子窗口的背景颜色
widget1->setStyleSheet("background-color: red;");
widget2->setStyleSheet("background-color: green;");
widget3->setStyleSheet("background-color: blue;");
// 设置子窗口的布局和内容
QVBoxLayout *layout1 = new QVBoxLayout(widget1);
QLabel *label1 = new QLabel(tr("Sub Window 1"), widget1);
layout1->addWidget(label1);
QVBoxLayout *layout2 = new QVBoxLayout(widget2);
QLabel *label2 = new QLabel(tr("Sub Window 2"), widget2);
layout2->addWidget(label2);
QVBoxLayout *layout3 = new QVBoxLayout(widget3);
QLabel *label3 = new QLabel(tr("Sub Window 3"), widget3);
layout3->addWidget(label3);
// 关联信号和槽函数
connect(toolButton->menu(), SIGNAL(triggered(QAction*)), this, SLOT(on_toolButton_clicked()));
connect(menu1, SIGNAL(triggered()), this, SLOT(on_menu1_triggered()));
connect(menu2, SIGNAL(triggered()), this, SLOT(on_menu2_triggered()));
connect(menu3, SIGNAL(triggered()), this, SLOT(on_menu3_triggered()));
// 设置主窗口的大小和标题
setCentralWidget(stackedWidget);
setFixedSize(400, 300);
setWindowTitle(tr("QStackedWidget Example"));
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_toolButton_clicked()
{
toolButton->showMenu();
}
void MainWindow::on_menu1_triggered()
{
stackedWidget->setCurrentWidget(widget1);
}
void MainWindow::on_menu2_triggered()
{
stackedWidget->setCurrentWidget(widget2);
}
void MainWindow::on_menu3_triggered()
{
stackedWidget->setCurrentWidget(widget3);
}
```
阅读全文