#include "mainwindow.h"
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include<QDebug>
#include<QToolBar>
#include<QPushButton>
#include<QStatusBar>
#include<QLabel>
#include<QTextEdit>
#include<QDockWidget>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//this->setWindowTitle("TEST");
this->resize(500,500);
//菜单栏////////////////////
QMenuBar *mBar = menuBar();
//添加菜单
QMenu *pFile = mBar->addMenu(QStringLiteral("文件"));
//添加菜单项,即菜单下面的动作
QAction *pNew = pFile->addAction(QStringLiteral("新建"));
connect(pNew,&QAction::triggered,
[=]()
{
qDebug()<<QStringLiteral("“新建”被按下");
}
);
pFile->addSeparator(); //添加分割线
QAction *pOpen = pFile->addAction(QStringLiteral("打开"));
//工具栏/////////////////,通常都是菜单栏里一些常用的操作
QToolBar *toolBar = addToolBar("toolBar");
toolBar->addAction(pNew);//“新建”存在于工具栏
QPushButton *b = new QPushButton(this);
b->setText(QStringLiteral("打开"));
toolBar->addWidget(b);//“打开”以按钮的形式存在于工具栏
connect(b,&QPushButton::clicked,[=](){b->setText("OPEN");});
//状态栏////////////////////
QStatusBar *sBar=statusBar();
QLabel *label = new QLabel(this);
label->setText(QStringLiteral("状态1"));
//addWidget从左往右添加
sBar->addWidget(label);
sBar->addWidget(new QLabel(QStringLiteral("状态2"),this));
//addPermanentWidget从右往左添加
sBar->addPermanentWidget(new QLabel(QStringLiteral("状态3"),this));
sBar->addPermanentWidget(new QLabel(QStringLiteral("状态4"),this));
//核心控件///////////////////////////////
QTextEdit *textE = new QTextEdit(this);
setCentralWidget(textE);
//浮动窗口
QDockWidget *dockW = new QDockWidget(this);
addDockWidget(Qt::LeftDockWidgetArea,dockW);
QTextEdit *textE2 = new QTextEdit(this);
dockW->setWidget(textE2);
}
MainWindow::~MainWindow()
{
}
Qt--创建窗口菜单栏、工具栏、状态栏等
最新推荐文章于 2024-08-31 20:00:00 发布
该博客介绍了如何使用Qt库创建一个包含菜单栏、工具栏、状态栏和核心控件(如QTextEdit)的GUI应用程序。通过添加菜单项、工具栏按钮和浮动窗口,展示了Qt界面的构建过程。
1121

被折叠的 条评论
为什么被折叠?



