(九)标准对话框

Qt为开发者提供了一些可复用的对话框类型

Qt提供的可复用对话框全局继承自QDialog类


使用方法:

Qt中所有标准对话框遵循相同的使用方式

定义对话框对象->设置对话框属性->获取对话框数据(与用户交互后)->处理对话框数据

 

消息对话框(QMessageBox):

作用:

1、为用户提示重要信息

2、强制用户进行操作选择

 

使用:

         //不需要与用户交互,只提示信息(此种方式下,Qt会提供一个默认的ok按钮)

   QMessageBox msg(this);

 

   msg.setText("This is a message dialog!");

 

   msg.exec();     

 

         //强制用户选择

   QMessageBox msg(this);

 

   msg.setWindowTitle("Window Title");

   msg.setText("This is a detail message dialog!");

   msg.setIcon(QMessageBox::Information);                                //设置消息对话框图标

msg.setStandardButtons(QMessageBox::Ok| QMessageBox::Cancel | QMessageBox::YesToAll);   

/*

   if( msg.exec() == QMessageBox::Ok )

    {

       //………

}

*/

         intret = msg.exec();

         switch(ret)

{

         casex:

                   //………….

         default:

                   //…………

}

 

静态函数:

关于对话框

void

about ( QWidget * parent, const QString & title, const QString & text )

此函数在四个位置查找合适的图标:

(1)、如果parent->icon()存在,它会首先使用它

(2)、如果没有,它会尝试一下包含parent的顶级窗口部件

(3)、如果这个也失败了,它会尝试主窗口部件

(4)、作为最后的手段,它使用信息图标

默认提供一个ok按钮

 

危险对话框

StandardButton QMessageBox::critical( QWidget * parent, const QString & title, const QString & textStandardButtonsbuttons = Ok, StandardButton defaultButton =NoButton )

参数defaultButton指定当Enter按钮按下时buttons中的哪个按钮会被使用 ,如果使用默认参数NoButton将会自动选择一个合适的默认按钮

返回被点击的按钮,如果按下的是Esc,返回escape button

 

信息对话框

StandardButton QMessageBox::information( QWidget * parent,const QString & title,const QString & text,StandardButtons buttons =Ok, StandardButton defaultButton =NoButton )

 

询问对话框

StandardButton QMessageBox::question( QWidget * parent, const QString & title,const QString & textStandardButtonsbuttons =Ok, StandardButton defaultButton =NoButton )

 

警告对话框

StandardButton QMessageBox::warning( QWidget * parent,const QString & title,const QString & textStandardButtonsbuttons =Ok, StandardButton defaultButton =NoButton )

 

 

文件对话框(QFileDialog):

应用:

(1)、应用程序需要打开一个外部的文件

(2)、需要将当前内容存储到用户指定的外部文件中

 

文件对话框的过滤器

格式:”显示名(*.后缀1 *.后缀2 …… *.后缀N)”

如:   “Image(*.png *.xpm *.jpg)”             “Text(*.txt)”             “All(*.*)”

设置过滤器后只会显示设置的类型对象

只要设置过滤器,建议将“All(*.*)”一同设置

使用:

QFileDialogdlg(this);

 

         //dlg.setAcceptMode(QFileDialog::AcceptSave);            //存储模式

   dlg.setAcceptMode(QFileDialog::AcceptOpen);               //打开模式

   dlg.setFilter("Text(*.txt)");                                           //设置文件过滤器,只显示文本文件

    dlg.setFileMode(QFileDialog::ExistingFiles);            //一次获取多个存在的文件PathName

         //dlg.setFileMode(QFileDialog::ExistingFile);           //一次获取单个存在的文件PathName

 

   if( dlg.exec() == QFileDialog::Accepted )

    {

       QStringList fs = dlg.selectedFiles();

//……..

    }

 

静态成员函数:

获取已存在目录

QString QFileDialog::getExistingDirectory( QWidget * parent = 0,const QString & caption =QString(), const QString & dir= QString(), Options options =ShowDirsOnly )

参数caption dir分别为标题与默认工作目录,他们中任何一个都可以为空,为空时当前目录与默认标题被分别使用

 

获取单个已存在文件

QString QFileDialog::getOpenFileName( QWidget * parent =0, const QString & caption =QString(), const QString & dir = QString(),const QString & filter =QString(), QString * selectedFilter =0, Options options =0 )

参数dir为工作目录,不同的是,如果dir包含了文件名,被包含的文件将被选择,参数为filter 文件过滤器,与过滤器匹配的文件类型才被显示,指定多个过滤器时使用双分号( ;; )隔开,如:    "Images (*.png *.xpm*.jpg);;Text files (*.txt);;XML files (*.xml)"

参数selectedFilter 为返回的用户选择的过滤器,参数options 决定如何运行对话框,具体查看枚举变量QFileDialog::Option

 

获取单个或多个已存在文件:

QString QFileDialog::getOpenFileNames( QWidget * parent =0, const QString & caption =QString(), const QString & dir = QString(),const QString & filter =QString(), QString * selectedFilter =0, Options options =0 )

 

获取存储文件(不需要已存在)

QString QFileDialog::getSaveFileName( QWidget * parent = 0,const QString & caption =QString(), const QString & dir = QString(),const QString & filter =QString(), QString * selectedFilter =0, Options options =0 )

 

 

颜色对话框(QColorDialog):

应用:提供指定颜色

 

QColor类(Qt中程序中的颜色):

QColor类支持多种颜色表示方式:

(1)、 RGB以红、绿、蓝为基准的三色模型

(2)、 HSV以色调、饱和度、明度为基准的六角锥体模型

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

使用:

QColorDialogdlg(this);

 

   dlg.setWindowTitle("Color Editor");

   dlg.setCurrentColor(QColor(100, 111, 222));          //设置初始颜色

 

   if( dlg.exec() == QColorDialog::Accepted )

    {

       QColor color = dlg.selectedColor();                   //获取选择的颜色

                   //…….

    }

 

静态成员函数

获取颜色

QColor QColorDialog::getColor(const QColor & initialQWidget * parent,const QString & titleColorDialogOptionsoptions =0 )

 

QColor QColorDialog::getColor( const QColor & initial =Qt::white, QWidget * parent =0 )

参数initial为初始颜色,未指定参数title时Qt会默认设置标题为 "Select Color" ,参数options 用于指定对话框外观,具体查看枚举变量QColorDialog::ColorDialogOption

注:当用户cancel对话框同样会返回一个invalid color值,需要通过成员函数boolQColor::isValid () const判断用户选择是否有效

 

 

输入对话框(QInputDialog)

应用:用于需要临时进行数据输入的场合

 

输入模式:

(1)、输入文本字符串QInputDialog::TextInput

(2)、输入整型值QInputDialog::IntInput

(3)、输入浮点值QInputDialog::DoubleInput

使用:

QInputDialogdlg(this);

 

   dlg.setWindowTitle("Input Test");

   dlg.setLabelText("Please input an integer:");

   dlg.setInputMode(QInputDialog:: IntInput);           //指定输入为整数

 

   if( dlg.exec() == QInputDialog::Accepted )

    {

       QString str = dlg. intValue();                                //获取整数值

                   //………

    }

 

静态成员函数:

输入浮点

double QInputDialog::getDouble( QWidget * parent,const QString & title,const QString & label,double value = 0, double min = -2147483647,double max = 2147483647, int decimals = 1,bool * ok = 0, Qt::WindowFlags flags =0 )

参数value 为编辑框上显示的默认值,参数min max 是用户可以输入的最小/最大值,参数decimals 指定小数点后最大位数,如果参数ok 不为空则返回用户选择,用户点击ok时,ok 所指向变量被置为true,点击cancel时,ok 所指向变量被置为false

 

输入整数

int QInputDialog::getInt( QWidget * parent,const QString & title,const QString & label,int value = 0, int min = -2147483647,int max = 2147483647, int step =1, bool * ok = 0, Qt::WindowFlags flags =0 )

参数step 为用户点击编辑框旁箭头时数值变化的步长

 

输入/选择项

QString QInputDialog::getItem( QWidget * parent,const QString & title,const QString & label,const QStringList & items,int current = 0, bool editable =true, bool * ok = 0, Qt::WindowFlags flags =0 )

参数items是插入到combobox中的字符串列表,参数current 指定当前项,参数editable 指定指定用户是否可以输入自己的文本,为true时,用户可输入自己的文本,否则用户只能从已存在项目中选择一个,函数返回当前项或用户输入(有输入时)的文本

例:

    QStringList items;

    items << tr("Spring") << tr("Summer")<< tr("Fall") << tr("Winter");

 

    bool ok;

    QString item = QInputDialog::getItem(this,tr("QInputDialog::getItem()"),

                                         tr("Season:"), items, 0, false, &ok);

    if (ok && !item.isEmpty())

        itemLabel->setText(item);

 

输入文本

QString QInputDialog::getText( QWidget * parent,const QString & title,const QString & labelQLineEdit::EchoModemode =QLineEdit::Normal, const QString & text =QString(), bool * ok = 0, Qt::WindowFlags flags =0 )

参数mode 指定编辑框的回显模式,具体查看枚举变量QLineEdit::EchoMode,参数text 指定编辑框内初始文本

例:

bool ok;

    QString text = QInputDialog::getText(this,tr("QInputDialog::getText()"),

                                         tr("User name:"), QLineEdit::Normal,

                                         QDir::home().dirName(), &ok);

    if (ok && !text.isEmpty())

        textLabel->setText(text);

 

字体对话框(QFontDialog)

应用:供用户选择字体

使用:

QFontDialogdlg(this);

 

   dlg.setWindowTitle("Font Dialog Test");

   dlg.setCurrentFont(QFont("Courier New", 10,QFont::Bold));//QFont的参数:字体名称,字体点大小,字体粗细

 

   if( dlg.exec() == QFontDialog::Accepted )

    {

       QFont font = dlg.selectedFont();

                   //……..

    }

 

静态成员函数

QFont QFontDialog::getFont(bool * ok, const QFont & initialQWidget * parent,const QString & titleFontDialogOptionsoptions )

 

QFont QFontDialog::getFont( bool * ok, const QFont & initialQWidget * parent, const char* name )

 

QFont QFontDialog::getFont( bool * okQWidget * parent,const char * name )

 

QFont QFontDialog::getFont( bool * ok, const QFont & initialQWidget * parent,const QString & title )

 

QFont QFontDialog::getFont( bool * ok, const QFont & initialQWidget * parent =0 )

QFont QFontDialog::getFont( bool * okQWidget * parent =0 )

参数指示用户点击的是ok还是cancel,点击ok将返回已选择的字体,点击cancel将返回参数initial,参数initial为初始的选择字体,参数options 指定对话框的构造方式,具体查看枚举变量QFontDialog::FontDialogOption

 

例:

bool ok;

 QFont font = QFontDialog::getFont(&ok,QFont("Times", 12), this);

 if(ok) {

    // font is set to the font the user selected

 }else {

    // the user canceled the dialog; font is set to the initial

    // value, in this case Times, 12.

 }

 

上述getFont重载组函数中的最后一个函数有些许区别,例:

bool ok;

 QFont font = QFontDialog::getFont(&ok,this);

 if(ok) {

    // font is set to the font the user selected

 }else {

    // the user canceled the dialog; font is set to the default

    // application font, QApplication::font()

 }

 

 

进度对话框(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

 

   dlg.exec();

 

 

打印对话框(QPrintDialog)

应用:设置打印相关的参数信息

使用:

   QPrintDialog dlg(this);

 

   dlg.setWindowTitle("Print Dialog Test");

 

   if( dlg.exec() == QPrintDialog::Accepted )

    {

       QPrinter* p = dlg.printer();

                   //useprinter object to print data

    }

QPrinter类:

1、打印设备及其参数的封装

2、封装了系统中打印设备的驱动接口

3、以相同的方式使用系统中的不同打印设备

 

 

Qt中标准对话框的设计模式

1、GUI界面部件产生数据对象

2、业务逻辑中的其他对象使用数据对象

3、GUI界面与业务逻辑通过数据对象连接

 

 

 

注:在使用对象构造对话框的情况下,当用户点击cancel时不会改变任何值,在使用静态成员函数构造对话框时,用户点击cancel时可能返回一直值,这可能影响某些东西



 声明:

此文根据 狄泰学院唐老师的《QT实验分析教程》创作,并根据自身理解与对官方文档对其进行了少许的扩展


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值