菜单栏
一个QMainWindow中最多包含一个菜单栏。
//创建并设置菜单栏
QMenuBar *bar = this->menuBar();
this->setMenuBar(bar);
//创建菜单
QMenu *fileMenu = bar->addMenu("文件");
QMenu *editMenu = bar->addMenu("编辑");
//创建菜单项及分割线
QAction *openAction = fileMenu->addAction("打开");
fileMenu->addSeparator();
QAction *newAction = fileMenu->addAction("新建");
工具栏
一个QMainWindow中可以包含多个工具栏
//创建并设置工具栏
QToolBar *toolBar = new QToolBar(this);
this->addToolBar(Qt::LeftToolBarArea, toolBar);
//设置工具栏可停留区域(分别为0x1, 0x2, 0x4, 0x8,可以按位或进行枚举)
toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);
//设置是否可以浮动
toolBar->setFloatable(false);
//设置是否可以移动
toolBar->setMovable(false);
//为工具栏添加QAction及分割线
QAction *closeAction = toolBar->addAction("关闭");
toolBar->addSeparator();
//为工具栏添加控件
toolBar->addWidget(new QPushButton("hello"));
状态栏
一个QMainWindow中最多包含一个状态栏
//创建并设置状态栏
QStatusBar *stBar = this->statusBar();
this->setStatusBar(stBar);
//为状态栏添加标签
QLabel *leftLabel = new QLabel("左侧提示信息", this);
QLabel *rightLabel = new QLabel("右侧提示信息", this);
stBar->addWidget(leftLabel);
stBar->addPermanentWidget(rightLabel);
铆接部件
又叫做浮动窗口,可以有多个
//创建并设置铆接部件(浮动窗口)
QDockWidget *dockWidget = new QDockWidget("铆接", this);
this->addDockWidget(Qt::BottomDockWidgetArea, dockWidget);
中心部件
//设置中心部件
QTextEdit *textEdit = new QTextEdit(this);
this->setCentralWidget(textEdit);