9.QT-標准對話框


Qt提供的可復用的標准對話框,全部繼承自QDialog類,如下圖所示:

  • QMessageBox:信息對話框,用於顯示信息、詢問問題等;
  • QFileDialog:文件對話框
  • QColorDialog:顏色對話框
  • QInputDialog:輸入對話框(允許用戶輸入一次數據)
  • QFontDialog:字體對話框
  • QProgressDialog:進度對話框
  • QPrintDialog:打印對話框
  • QPrintPreviewDialog:打印預覽對話框
  • QPageSetupDialog:打印設置對話框,為打印機提供紙張相關的選項

 

標准對話框使用方式

QDialogType dialog(this); //對話框對象的定義

dialog.setPropertyxxxx(value); //對話框屬性設置

if(dialog.exec() == QDialogType::vaule)
{
Type v = dialog.getDialogValue(); //獲取對話框數據
//... ... 處理數據

}

 

 

QMessageBox消息對話框
代碼如下:

#include <QtGui/QApplication>
#include <QDebug>
#include <QMessageBox> 
int main(int argc, char *argv[])
{ 
QApplication a(argc, argv);
QMessageBox msg;
msg.setWindowTitle("Message");
msg.setText("This is Message content!");
msg.setIcon(QMessageBox::Information);        //選擇圖標為:信息類型 
msg.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
msg.setButtonText(QMessageBox::Ok,QString("確定"));
msg.setButtonText(QMessageBox::Cancel,QString("取消"));
if(msg.exec()==QMessageBox::Ok)
{
qDebug()<<"QMessageBox::Ok"; //當點擊確定按鈕,則會打印信息
}
return 0;
}

效果:

 

也可以使用一個靜態函數來創建消息對話框:

StandardButton QMessageBox::information(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButtondefaultButton = NoButton);
//顯示信息對話框, 將顯示一個反嘆號圖標

StandardButton QMessageBox::question(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButtondefaultButton = NoButton);
//顯示問題對話框, 將顯示一個問號圖標

StandardButton QMessageBox::warning(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButtondefaultButton = NoButton);
//顯示警告對話框, 將顯示一個黃色嘆號圖標

StandardButton QMessageBox::critical(QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton= NoButton);
//顯示嚴重錯誤對話框, 將顯示一個紅色的錯誤符號 

void QMessageBox::about(QWidget * parent, const QString & title, const QString & text);
//顯示關於對話框,該對話框只有一個OK按鈕

比如:

#include <QtGui/QApplication>
#include <QDebug>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox msg;
int ret = msg.question(NULL,"Question","This is Message Question",QMessageBox::Ok|QMessageBox::Cancel);
if(ret==QMessageBox::Ok)
{
qDebug()<<"QMessageBox::Ok"; //當點擊Ok按鈕,則會打印qDebug
}
return 0;
}

效果:

 


QFileDialog文件對話框
QFileDialog的exec()返回值為QFileDialog::Accepted或者QFileDialog::Rejected

需要以下成員函數:

setAcceptMode (QFileDialog::AcceptOpen);    
            //設置文本對話框是用來打開文件(AcceptOpen),還是用來保存文件(AcceptSave)

setDirectory (const QString & directory );        //設置文本對話框路徑

setFileMode ( FileMode mode );
                     //設置文本對話框打開的文件模式,是單個文件?還是多個文件?還是包括目錄?
void  selectFile(const QString & filename);
        //預設置文件對話框默認打開的文件名
QStringList QFileDialog::selectedFiles();
          //用來獲取用戶選擇的多個 文件信息 位置(絕對路徑),比如:"C:/Users/Administrator/Desktop/新建文本文檔.txt" 
QString	selectedFilter();
      //獲取用戶選擇的文件過濾器

 

文件類型過濾器
通過setFilter()成員函數實現,文件過濾器定義規則如下:

  • 顯示名(*.后綴名1 *.后綴名2 ...*.后綴名n)

如果,有多個規則,則可以通過;;來隔開,例如:

setFilter("Image(*.jpg *.png *.bmp);;Text(*.txt);;All(*.*)"); 

 

以打開文件為例:

          QFileDialog dlg;

          dlg.setAcceptMode(QFileDialog::AcceptOpen);
          dlg.setFileMode(QFileDialog::ExistingFile);  //模式為:已存在的文件,如果為保存文件,則不需要指定模式
          dlg.setFilter("Image(*.jpg *.png *.bmp);;Text(*.txt);;All(*.*)");
          dlg.setDirectory("D:");
          dlg.selectFile("新建文件.txt");
        
          if(dlg.exec() == QFileDialog::Accepted)
          {
            QString Str=dlg.selectedFiles()[0];         //獲取用戶選擇的文件
            QString  filter = dlg.selectedFilter();    //獲取用戶選擇的過濾器

            qDebug()<<Str;
            qDebug()<<filter;
          }

 

也可以使用一個靜態函數直接打開文件,或者保存文件:

QString QFileDialog::getOpenFileName (
QWidget * parent = 0,    //父組件
const QString & caption = QString(),    //標題
const QString & dir = QString(),    //設置路徑, .表示當前路徑, /表示更目錄
const QString & filter = QString(), //過濾器
QString * selectedFilter = 0,    //用戶選擇文件后的過濾器信息
Options options = 0 );           //參數,它的取值為enum QFileDialog::Option  ,比如: QFileDialog::ShowDirsOnly (只顯示目錄)

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

 


QInputDialog輸入對話框
QInputDialog的exec()返回值為QFileDialog::Accepted或者QFileDialog::Rejected
常用函數:

setLabelText ( const QString & text );              //設置對話框標簽
setOkButtonText ( const QString & text ); //設置OK按鈕文本
setCancelButtonText ( const QString & text ) //設置Cancel按鈕文本
setInputMode ( InputMode mode); //設置輸入模式,是文本,還是整數,還是浮點數
setIntRange( int min, int max ); //如果是整數模式,則該函數可以設置允許整數范圍 setDoubleRange ( double min, double max ); //如果是浮點數模式,則該函數可以設置允許浮點數范圍
QString textValue (); //返回文本模式的輸入對話框返回值 int intValue (); //返回整數模式的輸入對話框返回值 double doubleValue (); //返回浮點數模式的輸入對話框返回值

 

示例:

QInputDialog log;
log.setInputMode(QInputDialog::TextInput);
log.setCancelButtonText("取消");
log.setOkButtonText("確定");
log.setWindowTitle("輸入ID");
log.setLabelText("請輸入ID");
if(log.exec()== QInputDialog::Accepted)
{
qDebug()<<log.textValue();
}

效果:

 

也可以使用一個靜態函數實現輸入對話框:

設置文本輸入對話框:

QString QInputDialog::getText( QWidget * parent, //父組件
const QString & title,                 //窗口標題
const QString & label,                 //標簽提示
QLineEdit::EchoMode mode = QLineEdit::Normal,    //輸入框模式
constQString & text = QString(),           //預定義的文本
bool * ok = 0,                       //如果用戶點擊ok,則ok =true
Qt::WindowFlags flags = 0 );

設置列表輸入對話框:

QString QInputDialog::getItem ( QWidget * parent, //父組件
const QString & title,                            //窗口標題
const QString & label,                            //標簽提示
const QStringList & items,                         //字符串鏈表,用來顯示列表
int current = 0,                                   //預定義,表示列表中第哪組
bool editable = true,                              //true表示列表可編譯
bool * ok = 0,                                    //如果用戶點擊ok,則ok =true
Qt::WindowFlags flags = 0 )

除此之外還有:getDouble(),getInt();

 

以getItem()為例:

QStringList items;
items << "Spring" << "Summer" << "Fall"<< "Winter";
bool ok;
QString item
= QInputDialog::getItem(0, "QInputDialog::getItem()","Season:", items, 0, false, &ok);
if (ok && !item.isEmpty()) qDebug()<<item;

效果:

 


QColorDialog顏色對話框
QColorDialog的exec()返回值為QFileDialog::Accepted或者QFileDialog::Rejected

需要用到以下函數:

void setCurrentColor(const QColor & color )    //設置打開后的顏色對話框預定顏色
                                    //它的取值可以為enum Qt::GlobalColor,   比如白色Qt::white
                                    //也可以為QColor類,    比如QColor(100,100,100),表示RGB(100,100,100)

QColor selectedColor();             //返回用戶選擇的哪個顏色,如果用戶點擊了退出,則返回的QColor::isValid()為false

bool QColor::isValid();             //顏色有效返回true,無效則返回false

 

示例:

 QColorDialog dlg;
dlg.setCurrentColor(Qt::white);
//預定義白色 dlg.exec(); QColor color=dlg.selectedColor(); if(color.isValid()) //如果用戶選擇了顏色 { qDebug()<<color.red(); qDebug()<<color.green(); qDebug()<<color.blue(); }

 

也可以使用一個靜態函數直接打開顏色對話框:

static QColor getColor(
   const QColor &initial = Qt::white, //預定義顏色
   QWidget *parent = 0);              //父組件

示例:

QColor color= QColorDialog::getColor(Qt::white);
 if(color.isValid()) //如果用戶選擇了顏色
{
  qDebug()<<color.red();
  qDebug()<<color.green();
  qDebug()<<color.blue();
}

 

QFontDialog字體對話框
QColorDialog的exec()返回值為QFileDialog::Accepted或者QFileDialog::Rejected

需要用到以下函數:

void setCurrentFont ( const QFont & font )    //設置打開后的字體對話框預定字體
                          //以"Courier New"字體為例,則參數填為font("Courier New",10,QFont::Bold,true);
                          //表示字體大小為10,加粗,斜體 
QFont QFontDialog::selectedFont (); //返回用戶選擇的字體

示例:

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QWidget w;
  w.setFixedSize(360,300);
  QLabel label("This is QLabel!",&w);
  w.show();
  QFontDialog dlg(
&w);   dlg.setFont(label.font());
  
if(dlg.exec()==QFontDialog::Accepted)   {     label.setFont(dlg.selectedFont());     label.adjustSize();   }
  
return a.exec(); }

選擇前-效果:

選擇后-效果:

 

也可以使用一個靜態函數直接打開字體對話框:

QFont QFontDialog::getFont( bool * ok, const QFont & initial, QWidget * parent, 
const QString & title,FontDialogOptions options ) ;

 


QProgressDialog進度條對話框

常用函數:

setMinimum(int minimum);             //設置進度條最小值
setMaximum(int maximum);        //設置進度條最大值
setValue(int value);          //設置當前進度值
setAutoClose(bool close);        //設置自動關閉,如果進度值=最大值時,則會自動關閉對話框
setLabelText(const QString &text); //設置標簽提示
setCancelButtonText(const QString &text); //設置Cancel按鈕提示
setCancelButton(QPushButton *button); //從新自定義按鈕框架,如果添NULL,則表示隱藏按鈕
setBar( QProgressBar * bar ); //從新自定義進度條框架
setLabel ( QLabel * label ); //從新自定義標簽

 

示例:

QProgressDialog dlg;
dlg.setMinimum(0);
dlg.setMaximum(1000);
dlg.setValue(520);
dlg.setWindowTitle("Update");
dlg.setLabelText("Update... ...");
dlg.setCancelButtonText("取消");
dlg.exec();

效果:

 

也可以直接通過構造函數設置:

QProgressDialog ( const QString & labelText, 
const QString & cancelButtonText, 
int minimum, 
int maximum, 
QWidget * parent = 0, 
Qt::WindowFlags f = 0 );

 

QPrintDialog打印機對話框

QPrinter類介紹:

  • QPrinter是打印設備及其參數的封裝
  • QPrinter封裝了系統中打印設備的驅動接口

試驗:

  QPrintDialog printDialog;
if (printDialog.exec() == QDialog::Accepted)
{
  QPrinter *printer=printDialog.printer(); //獲取Qprinter
  QTextDocument td;
  td.setPlainText("TEST TEST TEST"); //設置文本
  //printer->setOutputFileName("D:\\1.pdf"); //設置文件路徑
  td.print(printer); //打印td
}

效果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM