目录
一、自定义对话框
QDialog(及其子类,如QMessageDialog,以及所有Qt::Dialog类型的类)对于其 parent 指针都有额外的解释:如果 parent 为 NULL,则该对话框会作为一个顶层窗口;否则作为其父组件的子对话框(此时,其默认出现的位置是 parent 的中心)。顶层窗口与非顶层窗口的区别在于,顶层窗口在任务栏会有自己的位置,而非顶层窗口则会共享其父组件的位置。
对话框分为模态对话框和非模态对话框。
-
模态对话框:会阻塞同一应用程序中其它窗口的输入,操作其他窗口前需要关闭该对话框。
1.使用QDialog::exec()实现应用程序级别的模态对话框【当该种模态的对话框出现时,用户必须首先对对话框进行交互,直到关闭对话框,然后才能访问程序中其他的窗口】。
2.使用QDialog::open()实现窗口级别的模态对话框【该模态仅仅阻塞与对话框关联的窗口,但是依然允许用户与程序中其它窗口交互。窗口级别的模态尤其适用于多窗口模式】。
-
非模态对话框:允许在显示该对话框的同时,继续对其他窗口的内容进行编辑,如查找对话框。
1.使用QDialog::show()实现非模态对话框。
//点击新建按钮,创建模态对话框
connect(ui->actionnew,&QAction::triggered,this,[this](){
QDialog *dialog=new QDialog(this);
//当主窗口不关闭,即不进行析构时,多次打开关闭对话框会导致内存泄漏
dialog->setAttribute(Qt::WA_DeleteOnClose);//防止内存泄漏
dialog->resize(300,200);
dialog->setToolTip("这是个ToolTip");
dialog->setWindowIcon(QIcon(":new/texture/Images/Sunny.jpg"));
dialog->setWindowTitle("原生对话框");
dialog->exec();
});
//点击编辑按钮,创建非模态对话框
connect(ui->actionedit,&QAction::triggered,this,[this](){
//在堆上创建dialog
QDialog *dialog=new QDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose);//防止内存泄漏
dialog->show();
//在栈上创建dialog,一闪而过
QDialog dialog1(this);
dialog1.show();
});
二、标准对话框
标准对话框,是 Qt 内置的一系列对话框,用于简化开发。事实上,有很多对话框都是通用的,比如打开文件、设置颜色、打印设置等。这些对话框在所有程序中几乎相同,因此没有必要在每一个程序中都自己实现这么一个对话框。Qt 的内置对话框大致分为以下几类:
1.QMessageBox
QMessageBox是模态消息对话框,用于显示信息、询问问题等。
QMessageBox::about(this,"关于对话框","这时一个about对话框");
QMessageBox::aboutQt(this,"Qt About");
//严重错误对话框
QMessageBox::critical(this,"错误","这是个错误弹窗");
//提问对话框 利用返回值判断用户按键类型
if(QMessageBox::Save==QMessageBox::question(this,"提问","要保存吗?",QMessageBox::Save|QMessageBox::No,QMessageBox::No)){
//信息对话框
QMessageBox::information(this,"提示","保存成功");
}else{
//警告对话框
QMessageBox::warning(this,"警告","未保存");
}
自定义QMessageBox
QMessageBox *myBox=new QMessageBox(this);
myBox->setIcon(QMessageBox::Information);
myBox->setText("我吹过你吹过的晚风~");
myBox->setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
myBox->setDefaultButton(QMessageBox::Ok);
myBox->addButton(QMessageBox::Save);
myBox->setButtonText(QMessageBox::Ok,"确定");
myBox->setWindowTitle("歌词");
myBox->setDetailedText("详细信息:\n我吹过你吹过的晚风\n那我们算不算 相拥\n可如梦初醒般的两手空空\n心也空");
myBox->setInformativeText("描述信息:\n《错位时空》");
int ret = myBox->exec();
switch (ret)
{
case QMessageBox::Save:
qDebug() << "Save!";
break;
case QMessageBox::Ok:
qDebug() << "Ok!";
break;
case QMessageBox::Cancel:
qDebug() << "cancel!";
break;
}
2.QColorDialog
QColorDialog对话框是模态颜色对话框。
//颜色对话框 第一个参数为默认选的颜色
QColor color= QColorDialog::getColor(QColor(255,0,0),this,"选择颜色",QColorDialog::ShowAlphaChannel);
//修改label颜色
QPalette palette;
palette.setColor(QPalette::WindowText,color);
ui->label->setPalette(palette);
3.QFileDialog
QFileDialog是选择文件对话框。If you want multiple filters, separate them with ';;':
QString filepath= QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\fengchujun\\Downloads","txtFile(*.txt);;exeFile(*.exe)");
qDebug()<<filepath;
QAction *actionOpenFile=new QAction(QIcon(":new/texture/Images/sunny.png"),"打开文件",this);
QAction *actionSaveFile=new QAction(QIcon(":new/texture/Images/butterfly.png"),"保存文件",this);
QMenu *menu=menuBar()->addMenu("操作文件");
menu->addAction(actionOpenFile);
menu->addAction(actionSaveFile);
QTextEdit *edit=new QTextEdit("文本框",this);
setCentralWidget(edit);
connect(actionOpenFile,&QAction::triggered,this,[=](){
//文件对话框
QString filepath= QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\fengchujun\\Downloads","*.txt");
qDebug()<<filepath;
if(filepath.isEmpty())
{
QMessageBox::warning(this,"提示","您未选择文件");
}else{
QFile file(filepath);
if( file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
edit->setText(in.readAll());
file.close();
}else{
QMessageBox::warning(this,"提示","文件不可读");
}
}
});
connect(actionSaveFile,&QAction::triggered,this,[=](){
//文件对话框
QString filepath= QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\fengchujun\\Downloads","*.txt");
qDebug()<<filepath;
if(filepath.isEmpty())
{
QMessageBox::warning(this,"提示","您未选择文件");
}else{
QFile file(filepath);
if( file.open(QIODevice::WriteOnly))
{
QTextStream in(&file);
in<<"helloworld";
file.close();
}else{
QMessageBox::warning(this,"提示","文件不可写");
}
}
});
4.QFontDialog
QFontDialog是选择字体对话框。
bool ok;
QFont font= QFontDialog ::getFont(&ok,QFont("黑体",24),this,"选择字体");
if(ok)
{
qDebug()<<"点击了OK";
}else{
qDebug()<<"点击了Cancel";
}
qDebug()<<font.family()<<font.pointSize()<<font.bold();//字体 字号 是否加粗
5.QInputDialog
QInputDialog输入对话框,允许用户输入或者选择一个值(整数、浮点数、字符串、列表选项),并将其值返回。
QInputDialog *inputDialog_int=new QInputDialog(this);
bool getAge;
int age= inputDialog_int->getInt(this,"输入年龄","年龄",22,1,110,1,&getAge);
if(getAge)
{
qDebug()<<"年龄:"<<age;
}
QInputDialog *inputDialog_item=new QInputDialog(this);
QStringList list;
list<<"春天"<<"夏天"<<"秋天"<<"冬天";
bool getItem;
QString season= inputDialog_item->getItem(this,"选择季节","季节",list,0,false,&getItem);
if(getItem)
{
qDebug()<<"季节:"<<season;
}
QInputDialog *inputDialog_text=new QInputDialog(this);
bool getInfo;
QString info= inputDialog_text->getText(this,"密码","输入密码",QLineEdit::Password,"",&getInfo);
if(getInfo)
{
qDebug()<<"密码:"<<info;
}
6.QPageSetupDialog
QPageSetupDialog为打印机提供纸张相关的选项。
注意:需要在.pro文件添加printsupport库:QT += printsupport
QPrinter *printer=new QPrinter();
QPageSetupDialog * pageSetUpDialog=new QPageSetupDialog(printer,this);
if(pageSetUpDialog->exec()==QDialog::Accepted){
int ret=printer->pageSize();
QPrinter::ColorMode mode= printer->colorMode();
QPageLayout layout=printer->pageLayout();
qDebug()<<"纸张大小:"<<ret<<"颜色模式:"<<mode<<layout;
}
7.QPrintDialog
QPrintDialog 打印机配置
8.QPrintPreviewDialog
QPrintPreviewDialog 打印预览;
9.QProgressDialog
QProgressDialog 进度条对话框。
QProgressDialog *progressDialog=new QProgressDialog("进度","Cancel",0,100,this);
int value=0;
progressDialog->setValue(0);
QPushButton *btn=new QPushButton("控制进度条",this);
connect(btn,&QPushButton::clicked,this,[=]()mutable{
if(progressDialog->value()<100)
{
value+=10;
progressDialog->setValue(value);
}
});
progressDialog->exec();