112 lines
3.5 KiB
C++
112 lines
3.5 KiB
C++
#include "mainwindow.h"
|
||
#include <cstdio>
|
||
|
||
MainWindow::MainWindow(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
{
|
||
// 设置窗口的标题
|
||
setWindowTitle("正在学习QMainWindow");
|
||
resize(1200,960);
|
||
|
||
// 创建菜单栏(QMenuBar,QMenu,QAction)
|
||
QMenuBar *menu_bar = menuBar();
|
||
this->setMenuBar(menu_bar); // 设置当前窗口的菜单栏
|
||
|
||
// 创建图片空间
|
||
QPixmap pix_map;
|
||
pix_map.load(":icon/start");
|
||
|
||
// 创建菜单
|
||
QMenu *menu1 = new QMenu("文件",this);
|
||
// 创建菜单项
|
||
QAction *openAction = menu1->addAction("打开...");
|
||
QMenu *sub_menu = new QMenu("最近使用",this); // 子菜单
|
||
sub_menu->addAction("d:\\a.txt");
|
||
// 添加子菜单(菜单项也可以是一个菜单,即为子菜单)
|
||
menu1->addMenu(sub_menu);
|
||
QAction *newAction = menu1->addAction("新建...");
|
||
menu1->addSeparator();
|
||
newAction->setIcon(QIcon(pix_map));
|
||
QAction *exitAction = menu1->addAction("退出");
|
||
menu_bar->addMenu(menu1);
|
||
exitAction->setIcon(QIcon(":icon/stop"));
|
||
|
||
|
||
QMenu *menu2 = new QMenu("编辑",this);
|
||
QAction *editAction = menu2->addAction("复制");
|
||
menu2->addAction("剪切");
|
||
menu2->addAction("粘贴");
|
||
|
||
menu_bar->addMenu(menu2);
|
||
|
||
// 菜单项的快捷方式
|
||
openAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_O));
|
||
newAction->setShortcut(QKeySequence(tr("ctrl+n")));
|
||
exitAction->setShortcut(QKeySequence(tr("ctrl+q")));
|
||
|
||
// 初始化子控件
|
||
mEdit = new QTextEdit(this);
|
||
mEdit->hide();
|
||
mWidget1 = new Widget1;
|
||
|
||
connect(openAction,&QAction::triggered,[=](){
|
||
takeCentralWidget(); // 将当前中心空间从主窗口移除
|
||
qDebug()<<"正在打开文件...";
|
||
setCentralWidget(mEdit);
|
||
mEdit->show();
|
||
|
||
});
|
||
connect(newAction,&QAction::triggered,[=](){
|
||
takeCentralWidget();
|
||
qDebug()<<"正在新建文件...";
|
||
setCentralWidget(mWidget1);
|
||
|
||
});
|
||
connect(exitAction,&QAction::triggered,this,&QMainWindow::close);
|
||
|
||
// 工具栏
|
||
QToolBar *tool_bar = new QToolBar;
|
||
tool_bar->setParent(this);
|
||
tool_bar->addAction(openAction);
|
||
tool_bar->addAction(newAction);
|
||
tool_bar->addSeparator();
|
||
tool_bar->addAction(exitAction);
|
||
tool_bar->setMovable(false); // 禁止拖动
|
||
tool_bar->setFloatable(false); // 禁止浮动
|
||
tool_bar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
|
||
this->addToolBar(tool_bar);
|
||
// this->addToolBar(Qt::LeftToolBarArea,tool_bar);
|
||
|
||
|
||
// 创建状态栏
|
||
QStatusBar *status_bar = new QStatusBar(this);
|
||
setStatusBar(status_bar);
|
||
QLabel *label1 = new QLabel("状态: 1",this);
|
||
status_bar->addWidget(label1); // 从左向右添加
|
||
QLabel *label2 = new QLabel("状态: 2",this);
|
||
// label2->setFixedSize(200,40);
|
||
// label2->setStyleSheet("background-color: #cf0f13; padding-left: 100px;");
|
||
status_bar-> addPermanentWidget(label2); // 从右向左添加
|
||
status_bar->addWidget(new QLabel("状态 3: 0",this),1); // 1 是平铺剩余空间
|
||
|
||
|
||
// 创建铆接控件
|
||
QDockWidget *dock = new QDockWidget("铆接1",this);
|
||
dock->addAction(openAction);
|
||
dock->addAction(exitAction);
|
||
dock->setWidget(new Widget1(this));
|
||
QDockWidget *dock2 = new QDockWidget("铆接2",this);
|
||
dock2->setWidget(new QPushButton("点我试试",this));
|
||
addDockWidget(Qt::TopDockWidgetArea,dock);
|
||
addDockWidget(Qt::TopDockWidgetArea,dock2);
|
||
|
||
|
||
// 中心部件
|
||
|
||
}
|
||
|
||
MainWindow::~MainWindow()
|
||
{
|
||
}
|
||
|