Qt學習之路 詳解 下篇
本文開始介紹Qt學習之路,本文用了大量的類,這樣才能使我們可以很好的去了解Qt。
Subclassing QMainWindow(從QMainWindow 派生類)
closeEvent是QWidget的虛函數(shù), 當關閉窗口時自動調(diào)用, 在派生類中可以重新實現(xiàn)該函數(shù). Qt 應用程序使用圖像的方法: 保存圖像至文件, 運行期間加載 在源代碼中包含XPM文件(XPM文件也是有效的C++文件)
使用Qt資源機制
如果需要使用資源系統(tǒng), 我們必須創(chuàng)建資源文件, 并在.pro文件中添加一行標識資源文件: RESOURCES = spreadsheet.qrc
資源文件僅僅是簡單的XML格式
- <RCC>
- <qresource>
- <file>images/icon.png</file>
- ...
- <file>images/gotocell.png</file>
- </qresource>
- </RCC>
- <RCC>
- <qresource>
- <file>images/icon.png</file>
- ...
- <file>images/gotocell.png</file>
- </qresource>
- </RCC>
需要使用前綴 :/, 如 :/images/icon.png
2 、Creating Menus and Toolbars
在Qt中創(chuàng)建菜單和工具條有以下三個步驟:
創(chuàng)建和設置Action
創(chuàng)建菜單和并給他們放置action
創(chuàng)建工具條并給他們放置action
- QAbstractItemView:: selectAll()
- QTableView::setShowGrid(bool)
QMainWindow::menuBar () --- ***次調(diào)用則創(chuàng)建一個菜單條
widget增加右鍵菜單的方法:
首先addAction, 而后調(diào)用setContextMenuPolicy(Qt::ActionsContextMenu); 設置關聯(lián)菜單,是重載QWidget::contextMenuEvent函數(shù), 然后調(diào)用exec()實現(xiàn)
- QMainWindow::addToolBar() --- 增加工具條
3、 Setting Up the Status Bar
QMainWindow::statusBar ()函數(shù)得到其指針, 而后可用 addWidget() 添加該狀態(tài)欄
QStatusBar::addWidget() 第二個參數(shù)設置為1表示拉伸
#p#
4、Implementing the File Menu
QMessageBox::warning() --- 警告對話框, 還有 information(), question(), critical()
QFileDialog::getOpenFileName() 打開文件對話框. QFileDialog::getSaveFileName 保存文件對話框, QFileDialog::DontConfirmOverwrite()
文件對話框的窗口會出現(xiàn)其父窗口的左上角, 并共享其父窗口的任務條(taskbar entry)
QWidget的close() slot會調(diào)用closeEvent()
- event->ignore(); // 忽略該事件
- event->accept(); // 表示接受該事件
我們可以通過設置QApplication's quitOnLastWindowClosed 屬性為假來禁止程序關閉, 直至調(diào)用QApplication::quit()
- QFileInfo(fullFileName).fileName(); 得到文件名
Qt列表容器的prepend()函數(shù)用于列表, 列表類的方法之一, 作用是插入列表的開頭
QVariant類型可以保存許多C++和Qt類型的數(shù)據(jù), 可將該數(shù)據(jù)保存至 Action 的Data中QObject::sender() 該函數(shù)可以在slot中得到sender object的指針, 對于多個signal連接至一個slot時很有用.
5、Using Dialogs
通過signal和slot 對查找對話框和主程序進行互動
QWidget可以通過方法 raise() 和 activateWindow() 來使得該窗口激活在屏幕的最前方
非模式對話框使用show()來顯示, 模式對話框則使用 exec()來顯示
QTableWidgetSelectionRange --- 存儲表格選擇區(qū)域的左上和右下所在行列
About對話框 --- QMessageBox::about()
比較少的做法: QMessageBox 或 QFileDialog可以像正常的widget那樣創(chuàng)建, 而后調(diào)用exec執(zhí)行.
6、 Storing Settings
QSettings在不同的平臺中, 存儲在不同的地方. Windows程序則存儲在系統(tǒng)注冊表里.
其構造函數(shù)參數(shù)含組織名稱和應用程序名稱, 方便其查找和寫入
QSettings 存儲類 key-value對的設定, key類似文件系統(tǒng)路徑, subkey則類似路徑語法(如findDialog/matchCase)
可使用beginGroup()和endGroup()
- settings.beginGroup("findDialog");
- settings.setValue("matchCase", caseCheckBox->isChecked());
- settings.setValue("searchBackward", backwardCheckBox->isChecked());
- settings.endGroup();
- settings.beginGroup("findDialog");
- settings.setValue("matchCase", caseCheckBox->isChecked());
- settings.setValue("searchBackward", backwardCheckBox->isChecked());
- settings.endGroup();
QSetting的value則可以為int, bool, double, QString, QStringList, 或者任意QVariant支持的類型.
7、Multiple Documents
修改程序為多文檔程序
File|New: 創(chuàng)建一個新的空文檔窗口, 而不是重新使用已存的主窗口
File|Close: 關閉當前主窗口
File|Exit: 關閉所有窗口
給widget設置屬性Qt::WA_DeleteOnClose, 當關閉的時候刪除該widget在內(nèi)存中的資源, 節(jié)省內(nèi)存. setAttribute(Qt::WA_DeleteOnClose);
foreach (QWidget *win, QApplication::topLevelWidgets()); // 可以用來遍歷應用程序的所有窗口
8、Splash Screens
QSplashScreen實現(xiàn)Splash Screen效果
QSplashScreen在主窗口顯示之前顯示一張圖像, 并在圖像上寫信息用來告知用戶應用程序的初始化過程.
splash代碼一般位于main()函數(shù)中, 在調(diào)用QApplication::exec()之前
- QSplashScreen *splash = new QSplashScreen;
- splash->setPixmap(QPixmap(":/images/splash.png"));
- splash->show();
- Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
- splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);
- ... ...
- splash->showMessage(QObject::tr("Loading modules..."), topRight, Qt::white);
- ... ...
- splash->showMessage(QObject::tr("Establishing connections..."), topRight, Qt::white);
- ... ...
- splash->finish(&mainWin);
- delete splash;
- return app.exec();
小結:Qt學習之路內(nèi)容介紹完了,不知道有沒有幫助到你,也許你已經(jīng)對Qt很熟悉了,***希望本品按文章能幫助你很好的去學習,想了解更多請看: