lesson3-Qt對話方塊
一、QDialog類
1、對話方塊的概念
對話方塊在各種軟體中都會使用到,一般用來給使用者提示資訊或者接收使用者反饋的資訊,因此對話方塊是應用程式和使用者互動的平台。
對話方塊是一個頂層視窗,不能嵌入到其他視窗中。
2、對話方塊的種類
1)、強制回應對話方塊,該應用程式的其他視窗不能被訪問,必須等待目前的交談框消失,顯示模式對話方塊一般調用它的exec()函數
2)、非強制回應對話方塊,該應用程式的其他視窗還能繼續被訪問,顯示非強制回應對話方塊一般調用它的show()函數
3、QDialog類的父類是QWidget
二、QDialog的衍生類別
為了方便開發人員的使用,Qt對一些特殊功能的對話方塊做了封裝,提供一套標準的對話方塊。這些內建對話方塊提供靜態函數便於使用,通常都是調用系統本地的對話方塊
1、QFileDialog
使用方法:
1、開啟檔案對話方塊,返回選擇的檔案名稱
QString str = QFileDialog::getOpenFileName(
父視窗,
對話方塊名字,
預設選擇路徑,
檔案過濾器);
2、根據名字開啟檔案,成功返回true,失敗返回false
QFile file(str);
file.open(QIODevice::ReadWrite);
3、得到一個輸入資料流
QTextStream in(&file);
4、逐行讀出輸入資料流
in.readLine();
2、QColorDialog
使用方法:
1、擷取調色盤
QPalette palette = textEdit->palette();
2、開啟顏色對話方塊,擷取顏色
QColor color = QColorDialog::getColor(
palette.color(QPalette::Text), //對話方塊初始顏色
this //父視窗
);
3、設定調色盤顏色
palette->setColor(
QPalette::Text, //要設定的調色盤的部位
color //要設定的顏色
);
4、載入調色盤
textEdit->setPalette(palette);
GUI為不同的部位分別設定了顏色標誌
3、QFontDialog
使用方法:
1、開啟字型對話方塊,擷取字型
bool ok;
QFont font = QFontDialog::getFont(&ok);
如果點擊對話方塊的“確定”按鈕,那麼ok的值就會變為true;如果點擊對話方塊的“取消”按鈕,那麼ok的值就會變為false
2、設定字型
textEdit->setFont(font);
4、QInputDialog
使用方法:
開啟輸入對話方塊,輸入的內容會返回
QString str = QInputDialog::getText(
this, //父視窗
“inputDialog”, //視窗標題
“please input”, //輸入框上面的標籤文字
QLineEdit::Normal, //編輯框的顯示方式
QDir::home(), //編輯框預設的內容
ok //回填bool變數
)
5、QProgressDialog
QProgress::setRange(0,100) //設定進度條範圍
QProgress::setValue(50) //設定進度條當前值
三、QMessageBox
Qt提供了幾種顯示資訊的訊息框,這些訊息框都是模態對話方塊,平時在軟體裡面會經常用到
1、QMessageBox::question
一個具有標題和文本的訊息詢問框,開發人員可以根據具體需要定製按鈕的個數和按鈕的作用
2、QMessageBox::informat
一個具有標題和提示文本的提示訊息框,開發人員可以根據具體需要定製按鈕的個數和按鈕的作用
3、QMessageBox::warning
一個具有標題和文本資訊的警示訊息框,開發人員可以根據具體需要定製按鈕的個數和按鈕的作用
4、QMessageBox::critical
一個具有標題和文本資訊的致命資訊框,開發人員可以根據具體需要定製按鈕的個數和按鈕的作用
5、QMessageBox::about
一個具有標題和文本的訊息框
6、QMessageBox::aboutQt
顯示關於Qt的訊息框
7、訊息按鈕的制訂
四、QDialog執行個體
1、標頭檔
- #ifndef BUILDINDIALOG_H
- #define BUILDINDIALOG_H
- #include
- class buildInDialog : public QDialog
- {
- Q_OBJECT
- public:
- buildInDialog();
- private:
- QPushButton *fileBtn;
- QPushButton *colorBtn;
- QPushButton *fontBtn;
- QPushButton *saveBtn;
- QPushButton *closeBtn;
- QTextEdit *textEdit;
- private slots:
- void fileSlot();
- void colorSlot();
- void fontSlot();
- void saveSlot();
- void closeSlot();
- };
- #endif
2、實現檔案
- #include "buildInDialog.h"
- buildInDialog::buildInDialog()
- {
- fileBtn = new QPushButton("open");
- colorBtn = new QPushButton("color");
- fontBtn = new QPushButton("font");
- saveBtn = new QPushButton("save");
- closeBtn = new QPushButton("close");
- textEdit = new QTextEdit();
- //布局
- QVBoxLayout *vLay = new QVBoxLayout();
- QHBoxLayout *hLay = new QHBoxLayout();
- vLay->addWidget(fileBtn);
- vLay->addWidget(colorBtn);
- vLay->addWidget(fontBtn);
- vLay->addWidget(saveBtn);
- vLay->addWidget(closeBtn);
- hLay->addWidget(textEdit);
- hLay->addLayout(vLay);
- setLayout(hLay);
- connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
- connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
- connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
- connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
- connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
- }
- void buildInDialog::fileSlot()
- {
- //擷取檔案名稱字
- QString str = QFileDialog::getOpenFileName(this, "開啟檔案", "/", "All File(*.*)");
- //開啟檔案
- QFile file(str);
- if(!file.open(QIODevice::ReadWrite))
- return;
- //得到輸入資料流
- QTextStream in(&file);
- //讀取資料
- while(!in.atEnd())
- {
- QString st = in.readLine();
- textEdit->append(st);
- }
- }
- void buildInDialog::colorSlot()
- {
- //擷取條色板
- QPalette palette = textEdit->palette();
- //開啟對話方塊,擷取顏色
- QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);
- if(color.isValid())
- {
- //將顏色放到條色板
- palette.setColor(QPalette::Window, color);
- //載入調色盤
- textEdit->setPalette(palette);
- }
- }
- void buildInDialog::fontSlot()
- {
- bool ok;
- QFont font = QFontDialog::getFont(&ok);
- if(ok)
- textEdit->setFont(font);
- }
- void buildInDialog::saveSlot()
- {
- bool ok;
- //擷取輸入的資訊
- QString str = QInputDialog::getText(this, "輸入對話方塊", "請輸入名字", QLineEdit::Normal, "wj", &ok);
- //根據輸入的名字開啟檔案
- QFile file(str);
- file.open(QIODevice::WriteOnly);
- //擷取輸出資料流
- QTextStream out(&file);
- //將textEdit的內容寫入到out
- out<toPlainText()<<"\n";
- }
- void buildInDialog::closeSlot()
- {
- QProgressDialog *progress = new QProgressDialog();
- progress->setRange(0, 100);
- for(int i=0; i<=100; i+=10)
- {
- qApp->processEvents();
- progress->setValue(i);
- sleep(1);
- }
- }
3、主函數
- #include "buildInDialog.h"
- #include
- int main(int argc, char *argv[])
- {
- //設定編碼,防止漢字出現亂碼
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
- QApplication app(argc, argv);
- buildInDialog dialog;
- dialog.show();
- return app.exec();
- }
http://www.bkjia.com/PHPjc/1068089.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068089.htmlTechArticlelesson3-Qt對話方塊 一、QDialog類 1、對話方塊的概念 對話方塊在各種軟體中都會使用到,一般用來給使用者提示資訊或者接收使用者反饋的資訊,因此對...