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

Qt Designer教程 給自己編槽

移動開發(fā)
本文介紹的是Qt Designer教程 給自己編槽,Qt Designer我們已經(jīng)接觸的不少了,也有一定的了解。先來看內(nèi)容。

Qt Designer教程 給自己編槽是本文要介紹的內(nèi)容,QT Designer 的基本使用方法,并且知道來如何添加信號和槽了。這一章我們看看如何編寫自己的槽來響應(yīng)一些信號。好的我們開始這一章。不了解Qt Designer的友們,可參考:Qt Designer入門教程 初學(xué)者必看

  1. [root@localhost root]# mkdir /home/DesignerTutorial  
  2. [root@localhost root]# mkdir /home/DesignerTutorial/tutorial3  
  3. [root@localhost root]# cd /home/DesignerTutorial/tutorial3  
  4. [root@localhost tutorial3]# designer&  
  5. [1] 4233  
  6. [root@localhost tutorial3]# 

這里我們看到"Qt Designer - New/Open"對話框,這是一個新建對話框,我們先選擇對話框上的第一個"C++ Project",確定,然后更改名字,這里我們改為 tutorial3.pro 保存,默認(rèn)情況是保存到我們打開 designer 的目錄,也就是剛剛創(chuàng)建的文件夾 /home/DesignerTutorial/tutorial3這樣我們就創(chuàng)建了一個工程文件 *.pro。下面我們再創(chuàng)建一個對話框。

從菜單欄的 File->New 打開新建對話框,選擇第二個:Dialog,然后也同樣保存到剛剛創(chuàng)建的文件夾 /home/DesignerTutorial/tutorial3 下。這時侯,你的 QT Designer 中就會出現(xiàn)一個 From1 來. 更改右下角"Property Editor/Signal Handlers"那個窗口里的 caption ,原內(nèi)容 "Form1"改為 "我的第三個QT Desiger程序--編寫自己的槽"里的 name    , 原內(nèi)容 "Form1"改為 "form_main",可以看到這時候窗口的標(biāo)題欄文字"Form1"已經(jīng)改變?yōu)?"我的第三個QT Desiger程序--編寫自己的槽",不是嗎?

現(xiàn)在我們點(diǎn)擊一下左邊窗口上面的 ToolBox 中的那個 PushButton (標(biāo)有 OK 的圖標(biāo))并且用鼠標(biāo)選擇一個合適的大小. 在這個 PushButton 上面用用鼠標(biāo)雙擊一下,我們就可以改變按鈕上顯示的字了。這里我們把"pushButton1"這幾個字,換成 “加”, 然後用選擇 OK 就可以了。

現(xiàn)在我們看到, 那個按鈕上的標(biāo)簽已將變成 “加” 了. 更改右下角那個"Property Editor/Signal Handlers"窗口里的 name , 原內(nèi)容 "pushButton1"改為 "pushBtnAdd" 以同樣的方法,再放置一個按鈕PushButton到窗口上,改 pushButton2 為"關(guān)閉" , 將右下角那個窗口里的 name 的原內(nèi)容 "pushButton2"改為 "pushBtnClose" 。

現(xiàn)在我們放置兩個輸入數(shù)字的窗口,這個控件是 LineEdit 以同樣的方法放置兩個 LineEdit 到窗口上 ,更改右下角那個窗口里的 name 內(nèi)容 , 其中一個由原內(nèi)容 "lineEdit1" 改為 "lineEditAdd1"另一個由原內(nèi)容 "lineEdit2" 改為 "lineEditAdd2"   
  
我們再放置一個加法結(jié)果輸出的窗口,這個控件我們選擇另一個,它是 TextLabel 當(dāng)然你也可以選擇你喜歡的控件,比如:LineEdit,也可以。以同樣的方法放置一個 TextLabel 到窗口上 ,改 "textLabel1" 為"結(jié)果輸出" , 更改右下角那個窗口里的 name 內(nèi)容 , 原內(nèi)容 "textLabel1"改為 "textLabelResult"這里可以用鼠標(biāo)將其大小拉長些,一便容納我們的輸出數(shù)據(jù)。
    
我們這時侯需要給“關(guān)閉”按鈕一個 signal(信號), 這樣當(dāng)您點(diǎn)擊這個“關(guān)閉”按鈕的時侯,QT 才知道如何去處理這個信號。

我們按一下 F3(connect singnal slot)然後在那個“關(guān)閉”按鈕上面點(diǎn)擊一下,這時侯我們就看到了 Close Connection 的 Dialog 了. 在 Signal 中選擇 clicked, 在 slot 中,先選擇 close() 就好了,在 Reciever 中選擇 form_main. 這時侯選擇 OK.
       
下面我們自己寫個信號的處理槽來實(shí)現(xiàn)加法。

按一下 F3(或者有菜單欄選擇 Edit->Connections ),然後在那個 “加” 按鈕上點(diǎn)擊一下。

這時侯我們就看到了 "View and Edit Connection" 的 Dialog 了。

在 Signal 中選擇 clicked, 在 Receiver 中選擇 "form_main"

在 Slot 中,都是QT為我們準(zhǔn)備好的槽,我們可以不選擇,或者先選擇 close() 就好了。

不要關(guān)閉這個窗口,點(diǎn)擊右邊的“Edit Slots...”按鈕,然后就可以看見" Edit Functions "窗口,該窗口也可以通過“主菜單 Edit->Slots” 打開。

點(diǎn)擊 "New Function" 按鈕,將"newSlot()"改為 "addSlot()" ,然后選擇 OK。

這個時候我們再回到 "View and Edit Connection"對話框中,在 Slot 中我們現(xiàn)在看到了剛剛自己創(chuàng)建的槽"addSlot()",對嗎?是的,我們選擇"addSlot()"。

現(xiàn)在我們必須在"addSlot()"槽中添加數(shù)據(jù)處理的代碼,這樣它才能做我們期望的事情。

在主窗體"form_main"的空白處雙擊鼠標(biāo)左鍵,

彈出對話框"Creating ui.h file",我們選擇"Yes",然后可以看到一個編輯窗口,內(nèi)容如下:

  1. /*  
  2. ** ui.h extension file, included from the uic-generated form implementation.  
  3. **  
  4. ** If you wish to add, delete or rename functions or slots use  
  5. ** Qt Designer which will update this file, preserving your code. Create an  
  6. ** init() function in place of a constructor, and a destroy() function in  
  7. ** place of a destructor.  
  8. */  
  9. void form_main::addSlot()  
  10. {  

這正是我們需要的,不是嗎?因?yàn)槲覀冋刖庉?quot;addSlot()"這個槽。

好的我們在"addSlot()"函數(shù)體中添加如下代碼,那么這個文件的所有內(nèi)容看起來像這樣:

  1. /*  
  2. ** ui.h extension file, included from the uic-generated form implementation.  
  3. **  
  4. ** If you wish to add, delete or rename functions or slots use  
  5. ** Qt Designer which will update this file, preserving your code. Create an  
  6. ** init() function in place of a constructor, and a destroy() function in  
  7. ** place of a destructor.  
  8. */  
  9. void form_main::addSlot()  
  10. {  
  11.     QString str1, str2, strResult;  
  12.     double add1 = 0.0, add2 = 0.0, result = 0.0;  
  13.       
  14.     //得到輸入的字符  
  15.     str1 = lineEditAdd1->text();  
  16.     str2 = lineEditAdd2->text();  
  17.     //轉(zhuǎn)換為數(shù)字  
  18.     bool ok1 = FALSEok2 = FALSE;  
  19.     add1 = str1.toDouble( &ok1 );  
  20.     add2 = str2.toDouble( &ok2 );  
  21.     if ( ok1 &&  ok2 )  
  22.     { //如果輸入 的是數(shù)字的話  
  23.  result = add1 + add2;  
  24.  strResult.sprintf( "%f + %f = %f", add1, add2, result );  
  25.  textLabelResult->setText( strResult );  
  26.     }  
  27.     else  
  28.     {   
  29.  QMessageBox::warning( this, "Input Error",  
  30.         "Could not convert the input to the numbersn"  
  31.         "Please try again.nn",  
  32.         "Retry",  
  33.         "Quit", 0, 0, 1 );  
  34.     }  

因?yàn)槲覀儎倓傆玫搅薗String、QMessageBox這兩個類,我們必須包含他們的頭文件。

下面我們在"Object Explorer"中添加這個兩個類的頭文件,選擇"Menbers"標(biāo)簽,在"Includes (In declaration)"處,右擊鼠標(biāo)選擇"New",然后輸入:qstring.h,以同樣的方法輸入:qmessagebox.h
這時侯我們只需要一個小小的 main.cpp 就可以編譯了。同樣我們的 QT Designer 也可以為我們生成 main.cpp 。

從菜單欄的 File->New 打開新建對話框,選擇最后一個:C++ Main-File(main.cpp).

然后也同樣保存到剛剛創(chuàng)建的文件夾 /home/DesignerTutorial/tutorial3 下。

好了,現(xiàn)在來編譯我們的程序。

生成Makefile文件:

  1. [root@localhost tutorial3]# qmake 

編譯:

  1. [root@localhost tutorial3]# make  
  2. [root@localhost DesignerTutorial3]# ls  
  3. form_main.ui  main.cpp  Makefile  tutorial3  tutorial3.pro 

可以看到 tutorial3 ,這就是我們的可執(zhí)行程序,

運(yùn)行:

  1. [root@localhost tutorial3]# ./tutorial3 

在兩個輸入框中輸入數(shù)字,點(diǎn)擊“加”按鈕,看看效果再輸入非數(shù)字字符,看看效果。

你現(xiàn)在可以試著多添加幾個按鈕,分別寫上“加”“減”“乘”“除”等,

然后添加與其對應(yīng)的槽,去實(shí)現(xiàn)“加”“減”“乘”“除”等算法。

你一定可以做到的,我相信你能。Just do it!

總結(jié):Qt Designer教程 給自己編槽的內(nèi)容介紹完了,這里我們學(xué)習(xí)了在LinuxQT Designer 的基本使用方法,并且知道如何添加自己的信號處理槽。最后希望本文能幫你解決問題。

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

2011-07-04 11:21:59

QT Designer

2011-06-27 16:07:49

Qt Designer

2011-06-27 16:37:08

Qt Designer

2011-06-27 14:56:46

Qt Designer

2011-06-13 14:29:40

Qt Designer

2011-06-15 14:38:01

QT 信號

2011-07-04 11:29:40

QT Designer

2011-06-10 11:24:08

Qt Quick Designer

2011-06-09 09:45:35

Linux QT 信號

2011-07-04 13:17:18

Qt Designer 布局

2011-06-28 17:13:46

Qt Designer UI

2011-07-04 13:26:30

Qt Designer

2011-07-04 13:08:26

Qt Designer

2011-06-13 15:09:36

插件 Qt Designer

2021-12-23 15:07:40

QtC++編譯程序

2011-06-13 14:49:57

Qt Designer

2011-06-13 14:00:55

Qt Designer linux

2011-06-20 15:40:19

QT 信號

2011-06-20 15:52:14

Qt Designer 控件

2011-06-23 14:40:13

Qt 信號
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號