QT的标准对话框使用

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);
    
    //另一种使用定时器去更改值。
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值