//主要使用类<br />#include <Qstring>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QTextEdit>
#include <QDockWidget>
#include <QStatusBar>
#include <QLabel>
#include <QDialog>
#include <QMessageBox>
#include <QFileDialog>
QMenuBar类; 在windows平台下提供MenuBar()函数配合setMenuBar()使用;在mac中要建立共享菜单则要用new QMenuBar生成菜单栏;
QDialog类;其对象有模态与非模态窗口;可用exec()(使程序停留在此),或show()区分;在动作触发时创建的对象,可用setAttribute(设置对象属性)来设置消毁时机;<br />
QMessageBox类,QFileDialog类,对静态成员的使用;
QMessageBox类中对按键的选择判断可用switch; <br /><br />
//菜单栏
this->setMinimumSize(400, 200);
QMenuBar *myBar = menuBar();
this->setMenuBar(myBar);
QMenu *File = new QMenu(QString("文件"));
myBar->addMenu(File);
File->addAction(QString("新建"),[=]{
});
File->addSeparator();
File->addAction(QString("打开"), [=]{
QFileDialog::getOpenFileName(this, QString("打开文件"), QString("./"), QString("Images (*.png *.xpm *.jpg)"));
});
QAction *Abt = myBar->addAction(QString("模态"));
QAction *Abt1 = myBar->addAction(QString("非模态"));
//工具栏
QToolBar *ToBar = new QToolBar;
this->addToolBar(ToBar);
ToBar->addAction(QString("新建"),[=]{
});
ToBar->addAction(QString("打开"), [=]{
});
//工作区
QTextEdit *txt1 = new QTextEdit;
this->setCentralWidget(txt1);
//悬浮窗
QDockWidget *dock = new QDockWidget(QString("txt"));
QTextEdit *txt2 = new QTextEdit;
dock->setWidget(txt2);
this->addDockWidget(Qt::RightDockWidgetArea, dock);
//状态栏
QStatusBar *StBar = new QStatusBar;
this->setStatusBar(StBar);
QLabel *Lab1 = new QLabel(QString("状态栏:"));
QLabel *Lab2 = new QLabel(QString("12536"));
StBar->addWidget(Lab1);
StBar->addPermanentWidget(Lab2);
//模态对话框
connect(Abt, &QAction::triggered, [=]
{
QDialog *d1 = new QDialog;
d1->show();
d1->setAttribute(Qt::WA_DeleteOnClose);
});
//非模态
connect(Abt1, &QAction::triggered, [=]
{
QDialog *d2 = new QDialog;
d2->exec();
d2->setAttribute(Qt::WA_DeleteOnClose);
});
//MessageBox
QAction *about1 = myBar->addAction(QString("关于"));
connect(about1, &QAction::triggered, [=]
{
QMessageBox::about(this, QString("关于"), QString("关于qt"));
});
QAction *about2 = myBar->addAction(QString("保存/关闭"));
connect(about2, &QAction::triggered, [=]
{
QMessageBox::StandardButton i = QMessageBox::question(this, QString("关于2"), QString("多按键"),QMessageBox::Save | QMessageBox::Close);
switch(i)
{
case QMessageBox::Save:
{
Lab2->setText(QString("保存成功"));
}
break;
case QMessageBox::Close:
{
Lab2->setText(QString("关闭"));
}
break;
default:
break;
}
});
<br /><br />