QT編程技術(shù)詳解_第1頁(yè)
QT編程技術(shù)詳解_第2頁(yè)
QT編程技術(shù)詳解_第3頁(yè)
QT編程技術(shù)詳解_第4頁(yè)
QT編程技術(shù)詳解_第5頁(yè)
已閱讀5頁(yè),還剩105頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、QT GUI 編程 I、QT介紹 Qt 是一個(gè)跨平臺(tái)應(yīng)用程序和 UI 開發(fā)框架 只需一次性開發(fā)應(yīng)用程序,無(wú)須重新編寫源代碼,便可跨不同桌面和嵌入式操作系統(tǒng)部署這些應(yīng)用程 序 Qt完全的面向?qū)ο?、易于擴(kuò)展,并允許組件編程 Qt支持的平臺(tái) Embedded Linux Mac OS XWindowsLinux/X11 Windows CE/Mobile SymbianMeeGo I、QT介紹 QT類庫(kù) 模塊化 Qt C+ 類庫(kù)提供一 套 豐 富 的 應(yīng) 用 程 序 生 成 塊 (block),包含了生成高級(jí)跨 平臺(tái)應(yīng)用程序所需的全部功 能。 基于OpenGL與OpenGL ES的 3D 圖形 多線

2、程 嵌入式設(shè)備的緊湊視窗系統(tǒng) 對(duì)象間通訊 2D圖形 多媒體框架 WebKit 集成 網(wǎng)絡(luò)連接 XML 腳本引擎 數(shù)據(jù)庫(kù) 先進(jìn)的圖形用戶界面 I、QT介紹 QT官方網(wǎng)站 http:/ 相關(guān)學(xué)習(xí)資源 http:/ http:/ I、QT介紹 Qt 開發(fā)環(huán)境搭建 下載Qt SDK I、QT介紹 選擇相應(yīng)版本的Qt SDK下載 安裝 I、QT介紹 環(huán)境變量設(shè)置 添加路徑 一、Hello,World QT GUI工程創(chuàng)建 QT工程目錄結(jié)構(gòu) Hello World程序分析 例程 t1 一、Hello,World 頭文件 對(duì)于每個(gè)Qt類,都有一個(gè)與該類同名的頭文件,這個(gè)

3、頭文件中包括了該類的定義 #include :“QtGui”頭文件中包含了GUI程序設(shè)計(jì)中常用的類的頭文件 main( int argc, char *argv ) 程序入口 使用Qt編程時(shí),main()僅用于在進(jìn)入Qt library前的一些初始化工作 argc:命令行參數(shù)個(gè)數(shù) argv:命令行參數(shù)序列 一、Hello,World QApplication類 用于管理整個(gè)GUI應(yīng)用程序所用到的資源(resources)、控制流(control flow)和主要設(shè)置(main settings) contains the main event loop 每個(gè)GUI程序僅有一個(gè)QApplicat

4、ion對(duì)象 (無(wú)論該程序有多少個(gè)窗體) qApp宏 指向QApplication對(duì)象的全局指針 #define qApp QCoreApplication:instance() 常用屬性和函數(shù) int QApplication:exec() 進(jìn)入主事件循環(huán)(event loop),直到程序退出。 程序正常退出時(shí)返回 0 QString QCoreApplication:applicationDirPath () QString QCoreApplication:applicationFilePath () 返回程序路徑 二、 Calling it Quits 1. 更改按鈕字體 2. 加入點(diǎn)擊

5、按鈕退出程序功能 例程 t2 二、 Calling it Quits int main( int argc, char *argv ) . quit.setFont( QFont( Times, 18, QFont:Bold ) ); . QObject:connect( . 二、 Calling it Quits 窗口部件的字體屬性: 成員訪問函數(shù): const QFont QFont sansFont(Helvetica Cronyx, 12); QFont serifFont(); serifFont.setFamily(Times); serifFont.setPointSizeF(1

6、0); serifFont.setWeight(QFont:Bold); serifFont.setItalic(false); 二、 Calling it Quits 信號(hào)-槽(Signals 鼠標(biāo)點(diǎn)擊quit按鈕 quit對(duì)象發(fā)出信號(hào)click() 執(zhí)行對(duì)象a的quit()函數(shù) 二、 Calling it Quits 信號(hào)和槽是用于對(duì)象間通信的一種機(jī)制,它可以讓編程人員把那些互不了解 的對(duì)象綁定在一起 信號(hào)和槽機(jī)制是Qt主要特色之一 信號(hào)和槽機(jī)制取代傳統(tǒng)的CallBacks技術(shù) Signals and slots are used for communication between obj

7、ects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. A callback is a pointer to a function. If you want a processing function to notify you about some event, you pass a pointer to another function (th

8、e callback) to the processing function. Fundamental flaws: (1) not type-safe. (2) the callback is strongly coupled to the processing function (the processing function must know which callback to call.) 二、 Calling it Quits Qt類中預(yù)定義了大部分常用的信號(hào)與槽,同時(shí)用戶可以自定義信號(hào)與槽 信號(hào)的聲明 發(fā)出信號(hào) class 類名 : public QObject 或 其派生類 Q

9、_OBJECT . signals : 信號(hào)名(信號(hào)參數(shù)列表); . ; 1. 含有信號(hào)與槽的類必須繼承 自QObject類或其派生類 2. 類定義開始處需要有 Q_OBJECT宏 3. 在signals部分聲明信號(hào) 4. 信號(hào)的聲明與函數(shù)聲明相同, 但是沒有函數(shù)定義 emit 信號(hào)名(信號(hào)參數(shù)表); 二、 Calling it Quits 槽的聲明與定義 class 類名 : public QObject 或 其派生類 Q_OBJECT . 訪問權(quán)限 slots : 槽函數(shù)名(參數(shù)列表); . ; 1. 含有信號(hào)與槽的類必須 繼承自QObject類或其 派生類 2. 類定義開始處需要有 Q_

10、OBJECT宏 3. 在slots部分聲明槽 4. 訪問權(quán)限 public private protected 5. 槽與普通C+函數(shù)幾乎 完全一樣,唯一不同的 是它可以和信號(hào)連接 返回類型 類名:槽函數(shù)頭 槽函數(shù)定義 二、 Calling it Quits 信號(hào)與槽的連接 sender:指向發(fā)出信號(hào)的對(duì)象的指針 receiver:指向結(jié)束信號(hào)的對(duì)象的指針 1.一個(gè)信號(hào)可以連接多個(gè)槽,發(fā)射信號(hào)時(shí)會(huì)以不確定的順序自動(dòng)調(diào)用這些槽 2.多個(gè)信號(hào)可以連接同一個(gè)槽 3.一個(gè)信號(hào)可以與另外一個(gè)信號(hào)連接 4.連接可以被移除: QObject:disconnect (.) 5.連接在一起的信號(hào)和槽的參數(shù)必須具

11、有相同的順序和類型 信號(hào)與槽機(jī)制是通過Qt的元對(duì)象系統(tǒng)(meta-object system)的實(shí)現(xiàn)的 QObject:connect( sender, SINGAL( 信號(hào)名(參數(shù)類型表) ), receiver, SLOT( 槽名(槽參數(shù)類型表) ) ); 二、 Calling it Quits 信號(hào)-槽 例程 / counter.h #include class Counter : public QObject Q_OBJECT public: Counter() m_value = 0; int value() const return m_value; public slots: v

12、oid setValue(int value); signals: void valueChanged(int newValue); private: int m_value; ; / counter.cpp #include counter.h void Counter:setValue(int value) if (value != m_value) m_value = value; emit valueChanged(value); / main.cpp #include counter.h main() Counter a, b; QObject:connect( a.setValue

13、(12); / a.value()=12 / b.value()=12 b.setValue(48); / a.value()=12 / b.value()=48 QTEXP3_1 二、 Calling it Quits 信號(hào)和槽的擴(kuò)展閱讀 三、 Layout . layout-addWidget(formWidget); 三、 Layout layoutH-addWidget(pBtnOk); layoutH-addWidget(pBtnCancel); . . layoutH-addStretch(2); layoutH-addWidget(pBtnOk); layoutH-addWidg

14、et(pBtnCancel); layoutH-addStretch(1); . 三、 Layout layoutV-addWidget(pBtnTwo ); layoutV-addWidget(pBtnThree); layoutV-addSpacing(100); layoutV-addLayout(layoutH ); . 三、 Layout . layout-setRowMinimumHeight(1,200); . 三、 Layout void setMaximumSize ( int maxw, int maxh ); void setMinimumSize ( const QSi

15、ze void setMinimumSize ( int minw, int minh ); 三、 Layout pBtnThree-setMinimumSize( 200,50 ); 三、 Layout . MyWidget:MyWidget() / delte quit; ? Note that quit is a local variable in the constructor. MyWidget does not keep track of it, but Qt does, and will by default delete it when MyWidget is deleted.

16、 四、Create Your Own Widget QWidget The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface It receives mouse, keyboard and other events from the window system Paints a representation of itself on the screen. Every widget is rectangular They are

17、sorted in a Z-order. A widget is clipped by its parent and by the widgets in front of it. 四、Create Your Own Widget Top-Level and Child Widgets A widget without a parent widget is called a window (top- level widget). Usually, windows have a frame and a title bar(although it is also possible to create

18、 windows without such decoration using suitable window flags). For these widgets, setWindowTitle() and setWindowIcon() set the title bar and icon respectively. Non-window widgets are child widgets, displayed within their parent widgets. Most widgets in Qt are mainly useful as child widgets. 四、Create

19、 Your Own Widget QWidget 常用屬性和函數(shù) 構(gòu)造函數(shù) QWidget:QWidget ( QWidget * parent = 0, Qt:WindowFlags f = 0 ) Constructs a widget which is a child of parent, with widget flags set to f. QWidget * parent : The parent of the new widget. If it is 0 (the default), the new widget will be a window. If not, it will

20、 be a child of parent, and be constrained by parents geometry (unless you specify Qt:Window as window flag). Qt:WindowFlags f : Sets the window flags The default is suitable for almost all widgets You can use special flags. 四、Create Your Own Widget Qt:WindowFlags Qt:WindowFlags () const void ( Qt:Wi

21、ndowFlags type ) 四、Create Your Own Widget 四、Create Your Own Widget 擴(kuò)展閱讀 五、Create Composite Widget Composite Widgets When a widget is used as a container to group a number of child widgets, it is known as a composite widget. Composite widgets can be created by subclassing a standard widget, such as Q

22、Widget or QFrame, and adding the necessary layout and child widgets in the constructor of the subclass. 五、Create Composite Widget How to create composite widget How to create and connect together several widgets by using signals and slots How to use QLCDNumber How to use QSlider 例程 t5 五、Create Compo

23、site Widget QLCDNumber QLCDNumber常用屬性和函數(shù) 構(gòu)造函數(shù) The QLCDNumber widget displays a number with LCD-like digits. It can display decimal, hexadecimal, octal or binary numbers. QLCDNumber emits the overflow() signal when it is asked to display something beyond its range. QLCDNumber:QLCDNumber ( QWidget * p

24、arent = 0 ) Constructs an LCD number, sets the base to decimal, the decimal point mode to small and the frame style to a raised box. 五、Create Composite Widget QLCDNumber常用屬性和函數(shù) digitCount : int int digitCount()const void setDigitCount( int numDigits ) This property holds the current number of digits

25、 displayed. By default, this property contains a value of 5. 五、Create Composite Widget QLCDNumber常用屬性和函數(shù) value : double double value () const void display ( const QString . psldSlider: QSlider valueChanged(int) signal plcdLCD: QLCDNumber display(int)slot connect ( . ); 拖動(dòng)Slider Slider對(duì)象發(fā)出信號(hào) valueCha

26、nged(int) 執(zhí)行對(duì)象plcdLCD的 display(int)函數(shù) 六、 Update a widget at regular intervals How QTimer can be used to update a widget at regular intervals How to use QTime digitalclock 六、 Update a widget at regular intervals QTimer常用屬性和函數(shù) interval : int This property holds the timeout interval in milliseconds. Th

27、e default value for this property is 0. int interval () const void setInterval ( int msec ) void start ( int msec ) slot void singleShot( int msec , QObject * receiver, const char * member ) static 六、 Update a widget at regular intervals QTimer常用屬性和函數(shù) singleShot : bool This property holds whether th

28、e timer is a single- shot timer. A single-shot timer fires only once, non-single- shot timers fire every interval milliseconds. bool isSingleShot () const void setSingleShot ( bool singleShot ) void start ( int msec ) slot void singleShot( int msec , QObject * receiver, const char * member ) static

29、六、 Update a widget at regular intervals QTimer常用屬性和函數(shù) start ( ) : SLOT singleShot ( ) : STATIC stop( ) : SLOT void start ( int msec ) slot Starts or restarts the timer with a timeout interval of msec milliseconds. void start ( ) slot Starts or restarts the timer with the timeout specified in interva

30、l. void stop ( ) slot Stops the timer. void singleShot( int msec, QObject * receiver, const char * member ) static This static function calls a slot after a given time interval. 六、 Update a widget at regular intervals 擴(kuò)展閱讀 六、 Update a widget at regular intervals QTime The QTime class provides clock

31、time functions. A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock It provides functions for comparing times and for manipulating a time. 六、 Update a widget at regular intervals QTime常用

32、屬性和函數(shù) 創(chuàng)建對(duì)象 QTime () Constructs a null time object. QTime ( int h, int m, int s = 0, int ms = 0 ) Constructs a time with hour h, minute m, seconds s and milliseconds ms. QTime currentTime() static Returns the current time as reported by the system clock. QTime fromString ( const QString otherwise ret

33、urns false. QString QTime:toString ( const QString event-accept(); else event-ignore(); Sets / clears the accept flag of the event object Indicates that the event receiver wants / does not want the event. Unwanted events might be propagated to the parent widget. 七、The custom widget that can paint it

34、self Paint Event A paint event is a request to repaint all or part of a widget. It can happen for one of the following reasons: repaint() or update() was invoked The widget was obscured(遮蓋) and has now been uncovered void QWidget:paintEvent ( QPaintEvent * event ) virtual protected The QPaintEvent c

35、lass contains event parameters for paint events The event contains a region() that needs to be updated and a rect() that is the bounding rectangle of that region. 七、The custom widget that can paint itself QPainter The class performs low-level painting on widgets and other paint devices. It provides

36、highly optimized functions to do most of the drawing GUI programs require. The core functionality of QPainter is drawing The class also provide several functions that allows you to customize: QPainters settings Rendering quality Clipping Control how different shapes are merged(融合) together When the

37、paintdevice is a widget QPainter can only be used inside a paintEvent() function or in a function called by paintEvent() unless the Qt:WA_PaintOutsidePaintEvent widget attribute is set. 八、Canon Game Paint event() Mouse event() One thing leads to another Repaints a rectangle inside the widget. Using

38、timer to create animation 例程 cannon 九、常用技巧 I 1. 設(shè)置公司名稱、域名、程序名 int main(int argc, char *argv) QCoreApplication:setOrganizationName(HUST_EST); QCoreApplication:setOrganizationDomain(); QCoreApplication:setApplicationName(VideoArray); QCoreApplication:setApplicationVersion(Beta1.0.0); MyApplication a(a

39、rgc, argv); . 例程 QTEXP_8_1 九、常用技巧 I 2. 中文支持 int main(int argc, char *argv) . MyApplication a(argc, argv); QTextCodec *codec = QTextCodec:codecForName(GB2312); QTextCodec:setCodecForCStrings(codec); QTextCodec:setCodecForLocale(codec); QTextCodec:setCodecForTr(codec); . 例程 QTEXP_8_1 九、常用技巧 I 3. About

40、 Dialog info = ; info += + qApp-applicationName() + ; info += tr(版本:) + qApp-applicationVersion() + ; info += tr(版權(quán)所有:) + qApp-organizationName() + ; info += tr(organizationDomain() + tr( 公司主頁(yè) ); QMessageBox:about( this, qApp-applicationName(), info ); 例程 QTEXP_8_1 九、常用技巧 I 4.程序圖標(biāo) QSplashScreen spla

41、sh(pixmap); splash.show(); cessEvents(); splash.showMessage( QString(讀取系統(tǒng)配置信息.) ); cessEvents(); . Widget w; w.show(); splash.finish( return a.exec(); 例程 QTEXP_8_1 九、常用技巧 I 6. ini file / Open Ini File( AppPath/config.ini ) QString inifile = applicationDirPath() + /config.ini; QSettings set

42、tings(inifile, QSettings:IniFormat); / Read Setting QString user = settings.value(USER/USER,).toString(); / Write Setting settings.setValue( USER/USER , m_user ); 例程 QTEXP_8_1 九、常用技巧 I 7. system registry 1.Access system registry / Open QSettings reg(HKEY_CURRENT_USERControl PanelDesktop, QSettings:N

43、ativeFormat); / Set reg.setValue(AutoEndTasks,1); / Delete reg.remove(AutoEndTasks); 例程 QTEXP_8_2 九、常用技巧 I 7. system registry 2.Restoring the State of a GUI Application 例程 QTEXP_8_2 void Widget:closeEvent(QCloseEvent *event) QSettings reg( qApp-organizationName(), qApp-applicationName() ); reg.setVa

44、lue( Geometry, this-geometry() ); Widget:Widget(QWidget *parent) : QWidget(parent) . QRect rect; QSettings reg( qApp-organizationName(), qApp-applicationName() ); if( reg.contains(Geometry) ) rect = reg.value(Geometry).toRect(); setGeometry(rect); 九、常用技巧 I 8. MyApplication / / 重定義 qApp 為 MyApplicati

45、on 指針 /- #if defined(qApp) #undef qApp #endif #define qApp (static_cast(QCoreApplication:instance() / class MyApplication : public QApplication Q_OBJECT . 例程 QTEXP_8_1 十、常用技巧 II 1. Arguments int main(int argc, char *argv) . if(QCoreApplication:arguments().contains( udpdebug, Qt:CaseInsensitive ) . .

46、 例程 QTEXP_8_3 十、常用技巧 II 2.Reset Debug Output 1.Define Qt message handler 2. Install message handler int main(int argc, char *argv) . qInstallMsgHandler(myDebugOutput); . 例程 QTEXP_8_3 void myMessageOutput(QtMsgType type, const char *msg) switch (type) case QtDebugMsg : .; break; case QtWarningMsg : .

47、; break; case QtCriticalMsg: .; break; case QtFatalMsg : .; break; 十一、Network Programming The QtNetwork module offers classes that allow you to write TCP/IP clients and servers. It offers classes that implement specific application level protocols, e.g. QFtp It offers classes that implement specific

48、 lower- level protocols, e.g. QTcpSocket, QTcpServer and QUdpSocket It also offers classes that implement bearer management, such as QNetworkConfiguration, QNetworkConfigurationManager and QNetworkSession 例程 QTEXP_8_3 十二、Access File Open File Read Write Append Text File Read Write Bin File QFileDialog The QFile class provides an interface for reading from and writing to files. A QFile may be used by itself or, more conveniently, with a QTextStr

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論