QT 中提供的标准对话框:
QColorDialog
QFileDialog
QFontDialog
QInputDialog
QMessageBox
QProgressDialog
1 颜色选择对话框使用:
void ShowColorSelectDialog(){
QTextStream out(stdout);
//颜色对话框:
QColor color = QColorDialog::getColor(Qt::blue,this,"please select a color",QColorDialog::NoButtons);
if(color.isValid()){
out << "selected a color" << endl;
}
else{
out << "did not selected color" << endl;
}
}
2 文件对话框使用:
void ShowSelectFileDialog()
{
QTextStream out(stdout);
QString selectFile = QFileDialog::getOpenFileName(this,"Open file",R"(C:\Users\BZ\Desktop)","Images (*.png *.xpm *.jpg)",nullptr,QFileDialog::DontUseNativeDialog | QFileDialog::ReadOnly);
if(selectFile.isEmpty()){
out << "not select file" << endl;
}else{
out << "selected file is:" << selectFile << endl;
}
}
void ShowSaveFileDialog()
{
QTextStream out(stdout);
QString saveFilePath = QFileDialog::getSaveFileName(this,"save file",R"(C:\Users\BZ\Desktop)","Images (*.png *.xpm *.jpg)");
if(saveFilePath.isEmpty()){
out << "not set save file" << endl;
}else{
out << "save file path:" << saveFilePath << endl;
qDebug() << "save file path:" << saveFilePath << endl;
}
}
void ShowSelectFileDialog2()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
//dialog.setViewMode(QFileDialog::List);
dialog.setViewMode(QFileDialog::Detail);
dialog.setOptions(QFileDialog::DontUseNativeDialog);
dialog.setDirectory(R"(C:\Users\BZ\Desktop)");
//dialog.setLabelText(QFileDialog::LookIn,"this is a test");
dialog.setWindowTitle("this is a test");
//dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
}
3 字体选择对话框使用:
void ShowFontDialog()
{
QTextStream out(stdout);
bool ok = false;
QFont selectedfont = QFontDialog::getFont(&ok,this);
if(ok){
out << "selected font name:" << selectedfont.family() << endl;
}else{
out << "dit not select font" << endl;
}
}
4 输入对话框的使用(用于从用户哪里获取一个单一的输入值):
这里提供了4 种方式:getText、getItem、getInt、getDouble.
void MainWindow::ShowInputDialog()
{
QTextStream out(stdout);
bool ok = false;
//getText
// QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
// tr("User name:"), QLineEdit::Normal,
// QDir::home().dirName(), &ok);
// if (ok && !text.isEmpty()){
// out << "input text is:" << text << endl;
// }else{
// out << "not input" << endl;
// }
//getItem
// QStringList list{"item1","item2","item3","item4","item5"};
// QString selectedItem = QInputDialog::getItem(this,"getItem","item",list,0,false,&ok);
// if(ok && !selectedItem.isEmpty()){
// out << "selected item is:" << selectedItem << endl;
// }else{
// out << "not selected" << endl;
// }
//getInt
// int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
// tr("Percentage:"), 25, 0, 100, 1, &ok);
// if (ok){
// out << "select num is:" << i << endl;
// }else
// {
// out << "not select num" << endl;
// }
//getDouble
double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
if (ok){
out << "set num is:" << d << endl;
}else{
out << "not set num" << endl;
}
}
5 消息对话框使用:
void MainWindow::ShowMessageBox()
{
QTextStream out(stdout);
// //方式一
// QMessageBox box(this);
// QString str = QString::fromUtf16(u"这是一个测试");
// QString str2 = QString::fromUtf8("这是一个测试");
// box.setText(str);
// box.setInformativeText("hello world");
// box.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
// box.setDefaultButton(QMessageBox::Save);
// box.exec();
//方式二,使用静态函数。
// information(), question(), warning(), and critical()
int ret = QMessageBox::warning(this, tr("My Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel,
QMessageBox::Save);
out << "ret is:" << ret << endl;
}
6 进度条对话框使用:
void ShowProgressDialog()
{
// QProgressDialog progress("Copying files...", "Abort Copy", 0, 100, this);
// progress.setWindowModality(Qt::WindowModal);
// progress.show();
// for (int i = 0; i < 100; i++) {
// progress.setValue(i);
// Sleep(1000);
// if (progress.wasCanceled())
// break;
// //... copy one file
// }
// progress.setValue(100);
//另一种使用定时器去更改值。
}