自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt學習之路 詳解 下篇

移動開發(fā)
本文介紹對是Qt學習之路 Chapter詳解,用到了Qt 的很多類,本文實例是創(chuàng)建窗口,請看本文吧。

本文開始介紹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格式

  1. <RCC>   
  2.  <qresource>    
  3.     <file>images/icon.png</file>   
  4.      ...   
  5.       <file>images/gotocell.png</file> 
  6.   </qresource> 
  7.   </RCC> 
  8.   <RCC> 
  9. <qresource> 
  10.     <file>images/icon.png</file> 
  11.     ...  
  12.     <file>images/gotocell.png</file> 
  13. </qresource> 
  14. </RCC> 

 
需要使用前綴 :/, 如 :/images/icon.png

2 、Creating Menus and Toolbars

Qt中創(chuàng)建菜單和工具條有以下三個步驟:

創(chuàng)建和設置Action
創(chuàng)建菜單和并給他們放置action

創(chuàng)建工具條并給他們放置action

  1. QAbstractItemView:: selectAll()   
  2. 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)

  1. 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()

  1. event->ignore(); // 忽略該事件   
  2. event->accept(); // 表示接受該事件  

我們可以通過設置QApplication's quitOnLastWindowClosed 屬性為假來禁止程序關閉, 直至調(diào)用QApplication::quit()

  1. 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()

  1. settings.beginGroup("findDialog");  
  2.    settings.setValue("matchCase", caseCheckBox->isChecked());  
  3.    settings.setValue("searchBackward", backwardCheckBox->isChecked());  
  4.    settings.endGroup();  
  5.          settings.beginGroup("findDialog");  
  6. settings.setValue("matchCase", caseCheckBox->isChecked());  
  7. settings.setValue("searchBackward", backwardCheckBox->isChecked());  
  8. 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()之前

  1. QSplashScreen *splash = new QSplashScreen;  
  2.    splash->setPixmap(QPixmap(":/images/splash.png"));  
  3.    splash->show();  
  4.    Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;  
  5.    splash->showMessage(QObject::tr("Setting up the main window..."),       topRight, Qt::white);  
  6.    ... ...  
  7.    splash->showMessage(QObject::tr("Loading modules..."),       topRight, Qt::white);  
  8.    ... ...  
  9.    splash->showMessage(QObject::tr("Establishing connections..."),       topRight, Qt::white);   
  10.   ... ...  
  11.    splash->finish(&mainWin);  
  12.    delete splash;  
  13.    return app.exec();   

小結:Qt學習之路內(nèi)容介紹完了,不知道有沒有幫助到你,也許你已經(jīng)對Qt很熟悉了,***希望本品按文章能幫助你很好的去學習,想了解更多請看:

Qt學習之路 上篇

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關推薦

2011-06-17 09:58:26

Qt Chapter QObject

2011-07-05 18:40:19

QT 信號 機制

2011-06-30 10:36:07

QT Graphics View

2011-06-27 10:28:45

Qt 網(wǎng)絡 TCP

2011-06-24 16:50:06

QT 框架 OpenCV

2011-07-01 14:20:59

Qt 事件

2011-06-23 18:16:59

Qt Qtopia

2011-07-02 13:42:24

Qt 移植

2011-07-02 13:24:39

QT Linux

2011-07-05 13:32:45

QT Mysql 驅動

2010-05-18 13:53:34

Subversion插

2011-09-07 16:28:46

QT WidgetQWidget

2011-06-17 17:16:01

Qt Eclipse Linux

2011-06-17 13:39:47

Qt 文件

2011-09-01 16:01:25

Qt插件

2011-09-09 17:24:39

Qt Webkit模塊

2011-06-27 13:49:43

Qt 數(shù)據(jù)庫 QSqlQueryM

2011-06-30 16:08:05

Qt 字庫 QPF

2018-04-20 12:12:18

數(shù)據(jù)分析可視化數(shù)據(jù)挖掘

2016-12-15 21:15:58

大數(shù)據(jù)
點贊
收藏

51CTO技術棧公眾號