Qt內建了一些程式設計師常常會使用到的一些對話方塊,其中包含顏色選取檔案瀏覽等等。這些都屬於固定式的對話視窗,當你要使用時將可以直接使用Qt內建之對話方塊進行選取即可。本章捷將介紹Qt內建之(built-in)方塊。
Qt除了內建QMesseageBox訊息方塊外,Qt也提供了其他的內建方塊,其中包含
- QColorDialog:色彩選取工具。
- QErrorMessage:顯示錯誤訊息視窗。
- QFileDialog:檔案選取視窗。
- QFontDialog:使用者選取字型工具。
- QInputDialog:使用者簡單輸入工具。
- QPageSetupDialog:配置印表機選項。
- QProgressDialog:處理程序進度列。
- QPrintDialog:印表機列印方塊
 
Step1.
首先你要新增一個TextEdit及八個PushButton,相關Object名稱及屬性如下表所示,並將ui設計如下圖所示。
| Label | Object | ObjectName | 
| 
 | QTextEdit | displayTextEdit | 
| Color | QPushButton | color_B | 
| Message | QPushButton | Message_B | 
| File | QPushButton | File_B | 
| Font | QPushButton | Font_B | 
| Input | QPushButton | Input_B | 
| Page Setup | QPushButton | PageSetup_B | 
| Progress | QPushButton | Progress_B | 
| Print | QPushButton | Print_B | 
Step2.
include所需之檔案,於mainwindow.hpp中加入#include <QtGui/QDialog>,及mainwindow.cpp檔案中加入#include <QtGui/QtGui>。
Step3.
於個別button中加入相關程式碼。
Color
| QPalette palette = ui->displayTextEdit->palette(); const QColor& color
 = QColorDialog::getColor(palette.color(QPalette::Base), this);
 if(color.isValid())
 {
 palette.setColor(QPalette::Base, color);
 ui->displayTextEdit->setPalette(palette);
 }
 | 
 
 
.png) 
Message
| QErrorMessage box(this); box.setWindowTitle(tr("MessageBox"));
 box.showMessage(tr("MessageBox xx));
 box.showMessage(tr("MessageBox xx));
 box.showMessage(tr("MessageBox xx));
 box.showMessage(tr("MessageBox yy));
 box.showMessage(tr("MessageBox zz));
 
 box.exec();
 | 
執行結果如下圖所示。
 
File
| QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
 "/home/trong/",
 tr("Any File(*.*)"
 ";;Text File(*.txt)"
 ";;XML File(*.xml)"));
 ui->displayTextEdit->setText(fileName);
 | 
執行結果如下
Font
| bool ok; const QFont& font = QFontDialog::getFont(&ok,
 ui->displayTextEdit->font(),
 this,
 tr("Font Select box"));
 if(ok)
 {
 ui->displayTextEdit->setFont(font);
 }
 | 
執行結果
 
 
Input
| bool ok; QString text = QInputDialog::getText(this,
 tr("Input"),
 tr("Input Text:"),
 QLineEdit::Normal,
 QDir::home().dirName(),
 &ok);
 if (ok && !text.isEmpty())
 ui->displayTextEdit->setText(text);
 | 
執行結果
 
 
Page Setup
| QPrinter printer; QPageSetupDialog dlg(&printer, this);
 dlg.setWindowTitle(tr("Page Setup Box"));
 if (dlg.exec() == QDialog::Accepted)
 {
 // GO To Next process
 }
 | 
Progress
| QProgressDialog progress(tr("Processing..."), tr("Cancel"), 0, 10000, this); progress.setWindowModality(Qt::WindowModal);
 progress.setWindowTitle(tr("Processing Box"));
 progress.show();
 for (int i = 0; i < 10000; i++)
 {
 progress.setValue(i);
 qApp->processEvents();
 if (progress.wasCanceled())
 break;
 //
 qDebug() << i;
 }
 progress.setValue(10000);
 | 
 
 
 
Print Page
| QPrinter printer; QPrintDialog dlg(&printer, this);
 dlg.setWindowTitle(tr("Print Box"));
 if (dlg.exec() == QDialog::Accepted)
 {
 // next processing
 }
 | 
 
 
 
參考文獻:精通Qt4視窗介面程式開發與設計
 
 
 
沒有留言:
張貼留言