Qt 标准对话框

Qt提供了一些可以复用的对话框类型,这些对话框类型全部继承与QDialo类。

  

 Qt中标准对话框遵循相同的使用方法:

  //定义对话框对象

  DialogType dlg(this);

  //设置对话框属性

  dlg.setPrpertyXXX(value);

  if(dlg.exec() == DialogType::value)

  {

  //获取对话框数据

  Type V = dlg.getDialogValue();

  //处理对话框数据

  //....

  }

Qt消息对话框:QMessageBox

  QMessageBox 中的实用函数。

 

void    about ( QWidget * parent, const QString & title, const QString & text )
void    aboutQt ( QWidget * parent, const QString & title = QString() )
StandardButton    critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton    information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton    question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
StandardButton    warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

  测试代码:  

    QMessageBox Mes(this);

    /*消息提示对话框*/
//    if( Mes.information(this,"MessageBox","this is MessageBox!",QMessageBox::Ok,QMessageBox::Cancel,QMessageBox::YesAll)
//            == QMessageBox::Ok)
//    {
//        qDebug()<<"QMessageBox::Ok";
//    }
    /*警告提示对话框*/
//    if( Mes.warning(this,"MessageBox","this is MessageBox!",QMessageBox::Ok,QMessageBox::Cancel,QMessageBox::YesAll)
//            == QMessageBox::Ok)
//    {
//        qDebug()<<"QMessageBox::Ok";
//    }
    /*问题提示对话框*/
//    if( Mes.question(this,"MessageBox","this is MessageBox!",QMessageBox::Ok,QMessageBox::Cancel,QMessageBox::YesAll)
//            == QMessageBox::Ok)
//    {
//        qDebug()<<"QMessageBox::Ok";
//    }

    /*关键提示对话框*/
//    if( Mes.critical(this,"MessageBox","this is MessageBox!",QMessageBox::Ok,QMessageBox::Cancel,QMessageBox::YesAll)
//            == QMessageBox::Ok)
//    {
//        qDebug()<<"QMessageBox::Ok";
//    }
   Mes.about(this,"MessageBox","this is MessageBox!");

//    Mes.setWindowTitle("MessageBox");//设置标题
//    Mes.setText("this is MessageBox~");//设置提示内容
//    Mes.setIcon(QMessageBox::Warning);
//    Mes.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::YesAll);
//    if(Mes.exec() == QMessageBox::Ok)
//    {
//        qDebug()<<"QMessageBox::Ok";
//    }
    qDebug()<<"Messege_BtnClicked";

Qt文件对话框:QFileDialog

  Open Mode

    ——应用程序需要用户打开一个外部文件

  

 QFileDialog fl;
    fl.setAcceptMode(QFileDialog::AcceptOpen);//设置为Open模式
    QStringList setFs ;
    setFs <<"Text(*.txt)"<<"All(*.*)";


    fl.setFilters(setFs);

    /*QFileDialog::ExistingFile 打开单个
      QFileDialog::ExistingFiles 打开多个文件
    */
    fl.setFileMode(QFileDialog::ExistingFile);
    if(fl.exec() == QFileDialog::Accepted)
    {
        QStringList fs = fl.selectedFiles();//获取文件路径 文件名
        for(int i=0;i<fs.count();i++)
        {
            qDebug()<<fs[i];
        }
    }
    qDebug()<<"FileDialog_BtnClicked";

  Save Mode

    ——应用程序需要将当前内容存储到用户指定的目录中去

测试代码:

  

 QFileDialog fl;
    fl.setAcceptMode(QFileDialog::AcceptSave);
    fl.setFilter("Text(*.txt)");//设置保存属性
    if(fl.exec() == QFileDialog::Accepted)
    {
        qDebug()<<"QFileDialog::Accepted";
        QStringList fs = fl.selectedFiles();//获取文件路径 文件名
        for(int i=0;i<fs.count();i++)
        {
            qDebug()<<fs[i];
        }
    }
QString    getExistingDirectory ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly )
QString    getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
QStringList    getOpenFileNames ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
QString    getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )

颜色对话框:QColorDialog

  ——用于提供颜色的对话框部件 

  实验代码:

  

QColorDialog* color = new QColorDialog(this);
    if(color->exec() == QColorDialog::Accepted)
    {
       QColor nowColor = color->currentColor();//获取颜色信息
        qDebug() << nowColor;//ARGB模式显示
        /*获取RGB参数*/
        qDebug()<<nowColor.red();
        qDebug()<<nowColor.green();
        qDebug()<<nowColor.blue();
    }
    qDebug()<<"ColorDialog_BtnClicked";

  QColor类

    ——RGB:以红、绿、蓝为基准的三色模型

    ——HSV:以色调、饱和度、透明度、为基准的六角锥体模型

    ——CMYK:以天蓝、品红、黄色、黑为基准的全彩色印刷色彩模型

输入对话框:QInputDialog

  ——用于需要零时进行数据输入的场合

   

QInputDialog input;
    input.setLabelText("input double");//添加提示信息
    /*
      QInputDialog::TextInput
      QInputDialog::IntInput
      QInputDialog::DoubleInput
    */
    input.setInputMode(QInputDialog::DoubleInput);//设置输入属性
    input.setDoubleMaximum(1000);//设置输入数字最大值
    input.setDoubleMinimum(-1000);

    if(input.exec() == QInputDialog::Accepted)
    {
        qDebug()<<input.doubleValue();//获取输入值
    }
    qDebug()<<"InputDialog_BtnClicked";

字体对话框:QFontDialog

  ——用于提供选择字体的对话框部件

  

 QFontDialog font;
    if(font.exec() == QFontDialog::Accepted)
    {
       qDebug()<<font.currentFont();
    }
    qDebug()<<"FontDialog_BtnClicked";

进度对话框:QProgressDialog

   ——用于显示进度信息。需要用户等待的场合

 

   QProgressDialog progress;
    progress.setMaximum(100);//设置最大值
    progress.setValue(50);//设置进度值
    if(progress.exec() == QProgressDialog::Rejected)//关闭按钮处理
    {
        qDebug()<<"ProgressDialogEnd";
    }

    qDebug()<<"ProgressDialog_BtnClicked";

打印对话框:QPrintDialog

  ——用于设置打印相关的参数信息

 

QPrintDialog print ;
    if(print.exec() == QPrintDialog::Accepted)
    {
        QPrinter* p = print.printer();
        QTextDocument td; //使用QTextDocument
        td.setPlainText("hello,good");//设置字符信息
        p->setOutputFileName("I:\\test.pdf");//设置输出名称
        td.print(p);//打印文件
    }
    qDebug()<<"PrintDialog_BtnClicked";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DipsyHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值