Qt中的标准对话框
Qt中的标准对话框
一. 消息对话框
-
使用场景:
为用户提示重要信息
强制用户进行操作选择
-
基本使用
QMessageBox msg(this); msg.setWindowTitle("message dialog"); msg.setText("This is a message dialog!"); //setIcon可选择的图标 //QMessageBox::NoIcon //不显示图标,默认状态 //QMessageBox::Question //QMessageBox::Information //QMessageBox::Warning //QMessageBox::Critical msg.setIcon(QMessageBox::Critical); //添加标准的按钮 msg.setStandardButtons(QMessageBox::Ok|QMessageBox::Close|QMessageBox::SaveAll); msg.exec();
四个图标对应
添加按钮的效果
二. 文件对话框
-
使用场景:
打开文件 Open Mode
保存文件 Save Mode
-
基本使用
QFileDialog dlg(this); //定义一个文件对话框 //dlg.setAcceptMode(QFileDialog::AcceptSave); //设置为保存模式 //dlg.setFilter(QDir::Dirs); dlg.setAcceptMode(QFileDialog::AcceptOpen); //设置为打开模式 dlg.setFilter(QDir::Files); dlg.setFileMode(QFileDialog::ExistingFiles); if( dlg.exec() == QFileDialog::Accepted ) { QStringList fs = dlg.selectedFiles(); for(int i=0; i<fs.count(); i++) { qDebug() << fs[i]; } }
三. 颜色对话框
-
QColor表示颜色的方式
RGB: 红绿蓝为基准的三色模型
HSV: 以色调、饱和度、明度额外i基准的六角锥体模型
CMYK: 以天蓝、品红、黄色、黑为基准的全彩印刷色彩模型
-
使用示例
QColorDialog dlg(this); dlg.setWindowTitle("Color Editor"); //窗口标题 dlg.setCurrentColor(QColor(100, 111, 222)); //窗口内容 设置字体颜色 if( dlg.exec() == QColorDialog::Accepted ) { QColor color = dlg.selectedColor(); qDebug() << color; qDebug() << color.red(); qDebug() << color.green(); qDebug() << color.blue(); qDebug() << color.hue(); qDebug() << color.saturation(); qDebug() << color.value(); //饱和度 }
四. 输入对话框
-
使用场景:
需要临时进行数据输入的场合
-
使用实例
QInputDialog dlg(this); //定义输入对话框对象 dlg.setWindowTitle("Input Test"); //对话框标题 dlg.setLabelText("Please input a string:"); dlg.setInputMode(QInputDialog::TextInput); //数据类型为字符串 if( dlg.exec() == QInputDialog::Accepted ) { qDebug() << dlg.textValue(); }
五. 进度对话框( QProgressDialog)
-
使用场景:
用于进度显示
用于需要用户等待的场合
-
使用实例
//定义一个进度对话框对象 QProgressDialog dlg(this); //设置相关属性 dlg.setWindowTitle("Updating..."); //窗口标题 dlg.setLabelText("Downloading update from server..."); //消息内容 dlg.setMinimum(0); dlg.setMaximum(100); dlg.setValue(35); // create a new thread 启动一个后台的工作线程 //没有if语句,因为进度对话框比较特殊,用户只能在此处等待 dlg.exec();