標籤:qt c++ widget gui
一、工具列和菜單
只有主架構視窗(QMainWindow)才能夠添加菜單和工具列
QAction 命令
步驟:
1、添加action (動作編輯器) (在Qt Designer 介面的右下角)650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8B/69/wKiom1hNPKjy-YcBAAAjhEXygug328.png" title="QQ20161211194614.png" alt="wKiom1hNPKjy-YcBAAAjhEXygug328.png" />)
2、把action拖動到菜單或工具列裡
3、在代碼裡加上響應處理函數
範例程式碼:
Test10_1a_12_11::Test10_1a_12_11(QWidget *parent): QMainWindow(parent){ui.setupUi(this);assert(connect(ui.actionAct1,SIGNAL(triggered()),this,SLOT(OnHelpButton())));}int Test10_1a_12_11::OnHelpButton(){return 0;}
接下來實現一個簡單的應用。
二、單文檔程式
單文檔程式(Single Document):
只能編輯一個文檔,如: Notepad、Word
多文檔程式(Multiple Document):
可同時編輯多個文檔,如:Notepad++,UltraEdit , 使用選項卡,每頁代表一個文檔
做一個簡單的文字編輯器,實現以下功能:
1、開啟檔案
2、編輯檔案
3、儲存檔案
程式樣本:
Test10_1a_12_11::Test10_1a_12_11(QWidget *parent): QMainWindow(parent){ui.setupUi(this);assert(connect(ui.actionNew,SIGNAL(triggered()),this,SLOT(OnBtnNew())));assert(connect(ui.actionSave,SIGNAL(triggered()),this,SLOT(OnBtnSave())));assert(connect(ui.actionOpen,SIGNAL(triggered()),this,SLOT(OnBtnOpen())));}int Test10_1a_12_11::OnBtnNew(){ui.plainTextEdit->setEnabled(true);return 0;}int Test10_1a_12_11::OnBtnSave(){QString FilePath = QFileDialog::getSaveFileName(this, AfGbk::ToUnicode("儲存在..."));if (FilePath.length() == 0){return -1;}//儲存檔案string FileName = AfGbk::FromUnicode(FilePath);string text = AfGbk::FromUnicode(ui.plainTextEdit->toPlainText());//內容FILE* fp = fopen(FileName.c_str(), "wb");fwrite(text.c_str(), 1, text.length(), fp);fclose(fp);//狀態列顯示ui.statusBar->showMessage(AfGbk::ToUnicode("儲存成功..."), 3000);//狀態 顯示3秒return 0;}int Test10_1a_12_11::OnBtnOpen(){QString FilePath = QFileDialog::getOpenFileName(this, AfGbk::ToUnicode("開啟檔案..."));if (FilePath.length() == 0){return -1;}//讀取檔案內容string FileNameStr = AfGbk::FromUnicode(FilePath);FILE* fp = fopen(FileNameStr.c_str(), "rb");char buf[1024];int n = fread(buf, 1, sizeof(buf), fp);buf[n] = ‘\0‘;//顯示到介面中QString text = AfGbk::ToUnicode(buf);ui.plainTextEdit->setPlainText(text);ui.plainTextEdit->setEnabled(true);return 0;}
介面:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8B/66/wKioL1hNWvfBNpgUAAAa10asCuE864.png" style="float:none;" title="2.png" alt="wKioL1hNWvfBNpgUAAAa10asCuE864.png" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/8B/6A/wKiom1hNWvfBZ8p_AAAdpHEXilU892.png" style="float:none;" title="3.png" alt="wKiom1hNWvfBZ8p_AAAdpHEXilU892.png" />
三、可停靠視窗 QDockWidget
在Qt中,可停靠視窗作為一種容器類型的控制項
【Qt學習筆記】10.主視窗 The Main Window