lesson3-Qt對話方塊_PHP教程

來源:互聯網
上載者:User

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、標頭檔
 
  1. #ifndef BUILDINDIALOG_H
  2. #define BUILDINDIALOG_H

  3. #include

  4. class buildInDialog : public QDialog
  5. {
  6. Q_OBJECT
  7. public:
  8. buildInDialog();
  9. private:
  10. QPushButton *fileBtn;
  11. QPushButton *colorBtn;
  12. QPushButton *fontBtn;
  13. QPushButton *saveBtn;
  14. QPushButton *closeBtn;

  15. QTextEdit *textEdit;
  16. private slots:
  17. void fileSlot();
  18. void colorSlot();
  19. void fontSlot();
  20. void saveSlot();
  21. void closeSlot();

  22. };



  23. #endif
2、實現檔案
 
  1. #include "buildInDialog.h"

  2. buildInDialog::buildInDialog()
  3. {
  4. fileBtn = new QPushButton("open");
  5. colorBtn = new QPushButton("color");
  6. fontBtn = new QPushButton("font");
  7. saveBtn = new QPushButton("save");
  8. closeBtn = new QPushButton("close");

  9. textEdit = new QTextEdit();


  10. //布局
  11. QVBoxLayout *vLay = new QVBoxLayout();
  12. QHBoxLayout *hLay = new QHBoxLayout();
  13. vLay->addWidget(fileBtn);
  14. vLay->addWidget(colorBtn);
  15. vLay->addWidget(fontBtn);
  16. vLay->addWidget(saveBtn);
  17. vLay->addWidget(closeBtn);

  18. hLay->addWidget(textEdit);
  19. hLay->addLayout(vLay);

  20. setLayout(hLay);

  21. connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
  22. connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
  23. connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
  24. connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
  25. connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
  26. }

  27. void buildInDialog::fileSlot()
  28. {
  29. //擷取檔案名稱字
  30. QString str = QFileDialog::getOpenFileName(this, "開啟檔案", "/", "All File(*.*)");

  31. //開啟檔案
  32. QFile file(str);
  33. if(!file.open(QIODevice::ReadWrite))
  34. return;
  35. //得到輸入資料流
  36. QTextStream in(&file);
  37. //讀取資料
  38. while(!in.atEnd())
  39. {
  40. QString st = in.readLine();
  41. textEdit->append(st);
  42. }
  43. }

  44. void buildInDialog::colorSlot()
  45. {
  46. //擷取條色板
  47. QPalette palette = textEdit->palette();
  48. //開啟對話方塊,擷取顏色
  49. QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);

  50. if(color.isValid())
  51. {
  52. //將顏色放到條色板
  53. palette.setColor(QPalette::Window, color);
  54. //載入調色盤
  55. textEdit->setPalette(palette);
  56. }

  57. }

  58. void buildInDialog::fontSlot()
  59. {
  60. bool ok;
  61. QFont font = QFontDialog::getFont(&ok);
  62. if(ok)
  63. textEdit->setFont(font);
  64. }

  65. void buildInDialog::saveSlot()
  66. {
  67. bool ok;
  68. //擷取輸入的資訊
  69. QString str = QInputDialog::getText(this, "輸入對話方塊", "請輸入名字", QLineEdit::Normal, "wj", &ok);

  70. //根據輸入的名字開啟檔案
  71. QFile file(str);
  72. file.open(QIODevice::WriteOnly);
  73. //擷取輸出資料流
  74. QTextStream out(&file);
  75. //將textEdit的內容寫入到out
  76. out<toPlainText()<<"\n";
  77. }

  78. void buildInDialog::closeSlot()
  79. {
  80. QProgressDialog *progress = new QProgressDialog();
  81. progress->setRange(0, 100);
  82. for(int i=0; i<=100; i+=10)
  83. {
  84. qApp->processEvents();
  85. progress->setValue(i);
  86. sleep(1);
  87. }
  88. }
3、主函數
 
  1. #include "buildInDialog.h"
  2. #include

  3. int main(int argc, char *argv[])
  4. {
  5. //設定編碼,防止漢字出現亂碼
  6. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
  7. QApplication app(argc, argv);

  8. buildInDialog dialog;
  9. dialog.show();

  10. return app.exec();
  11. }




http://www.bkjia.com/PHPjc/1068089.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068089.htmlTechArticlelesson3-Qt對話方塊 一、QDialog類 1、對話方塊的概念 對話方塊在各種軟體中都會使用到,一般用來給使用者提示資訊或者接收使用者反饋的資訊,因此對...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.