Qt Designer 教程實(shí)例調(diào)用退出
Qt Designer 教程實(shí)例調(diào)用退出是本文要介紹的內(nèi)容,學(xué)習(xí)了 QT Designer 的基本使用方法,這一章我們添加一些稍稍復(fù)雜的功能。現(xiàn)在我們做一些準(zhǔn)備工作,上次我們說過,我們最好為每一個(gè)QT程序建立一個(gè)單獨(dú)的文件夾,
兩篇相關(guān)的Qt Designer 實(shí)例文章:
Qt Designer教程實(shí)例 HelloWorld
這樣就可以讓我們更方便的使用QT為我們提供的工具,例如qmake等。
- [root@localhost root]# mkdir /root/DesignerTutorial2
- [root@localhost root]# cd /root/DesignerTutorial2
然后打開 QT Designer ,直接在命令行下輸入:designer,如下:
- [root@localhost DesignerTutorial2]# designer&
- [3] 16544
- [root@localhost DesignerTutorial2]#
這里我們看到"Qt Designer - New/Open"對話框,這是一個(gè)新建對話框,我們先選擇對話框上的第一個(gè)"C++ Project",確定,然后更改名字,這里我們改為 tutorial2.pro 保存
默認(rèn)情況是保存到我們剛剛創(chuàng)建的文件夾 /root/DesignerTutorial2
這樣我們就創(chuàng)建了一個(gè) *.pro 文件。下面我們再創(chuàng)建一個(gè)對話框。
從菜單欄的 File->New 打開新建對話框,選擇第二個(gè):Dialog,
然后也同樣保存到剛剛創(chuàng)建的文件夾 /root/DesignerTutorial2 下。
這時(shí)侯,你的 QT Designer 中就會(huì)出現(xiàn)一個(gè) From1 來.
更改右下角那個(gè)窗口里的 caption ,原內(nèi)容 "Form1"改為 "我的第二個(gè)QT Desiger程序--調(diào)用退出",
name , 原內(nèi)容 "Form1"改為 "form_main"
可以看到這時(shí)候窗口的標(biāo)題欄文字已經(jīng)改變?yōu)?quot;Form1"改為 "我的第二個(gè)QT Desiger程序--調(diào)用退出",不是嗎?現(xiàn)在我們點(diǎn)擊一下左邊窗口上面的 ToolBox 中的那個(gè) PushButton (標(biāo)有 OK 的圖標(biāo))
并且用鼠標(biāo)選擇一個(gè)合適的大小. 在這個(gè) PushButton 上面用用鼠標(biāo)雙擊一下,我們就可以改變按鈕上顯示的字了。這里我們把"pushButton1"這幾個(gè)字,換成 “退出我的程序”,然後用選擇 OK 就可以了,現(xiàn)在我們看到, 那個(gè)按鈕上的標(biāo)簽已將變成 “退出我的程序” 了.
更改右下角那個(gè)窗口"Property Editor"里的 name , 原內(nèi)容 "pushButton1"改為 "pBtnClose"
現(xiàn)在我們想在程序運(yùn)行的時(shí)候點(diǎn)擊這個(gè) “退出我的程序” 按鈕就關(guān)閉我們的應(yīng)用程序,那么我們該怎么做呢?
也許你已經(jīng)想到了為它添加一個(gè)信號和槽,是這樣的嗎?那么恭喜你,你已經(jīng)了解了QT的核心內(nèi)容了。
我們按一下 F3(或者有菜單欄選擇 Edit->Connections ),然後在那個(gè) “退出我的程序” 按鈕上點(diǎn)擊一下。
這時(shí)侯我們就看到了 View and Edit Connection 的 Dialog 了. 在 Signal 中選擇 clicked, 在 slot 中,先選擇 close() 就好了,在 Reciever 中選擇 form_main. 這時(shí)侯選擇 OK. 。
我們就算是基本完成了。保存下。 如果想看看這個(gè)小程序長什麼樣子,可以用 CTRL+T來看 PreView. 點(diǎn)擊按鈕看看,窗口關(guān)閉了,對嗎?
這時(shí)侯我們只需要一個(gè)小的 main.cpp 就可以編譯了。同樣我們的 QT Designer 也可以為我們生成 main.cpp 。
從菜單欄的 File->New 打開新建對話框,選擇最后一個(gè):C++ Main-File(main.cpp).,然后也同樣保存到剛剛創(chuàng)建的文件夾 /root/DesignerTutorial2 下。好了,現(xiàn)在來編譯我們的程序。
生成Makefile文件:
- [root@localhost DesignerTutorial2]# qmake
編譯:
- [root@localhost DesignerTutorial2]# make
- /usr/lib/qt-3.1/bin/uic form_main.ui -o .ui/form_main.h
- g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions
- -DQT_NO_DEBUG -I/usr/lib/qt-3.1/mkspecs/default -I. -I/usr/lib/qt-3.1/include -I.ui/ -I.moc/ -o .obj/main.o main.cpp
- /usr/lib/qt-3.1/bin/uic form_main.ui -i form_main.h -o .ui/form_main.cpp
- g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions
- -DQT_NO_DEBUG -I/usr/lib/qt-3.1/mkspecs/default -I. -I/usr/lib/qt-3.1/include -I.ui/ -I.moc/ -o .obj/form_main.o .ui/form_main.cpp
- /usr/lib/qt-3.1/bin/moc .ui/form_main.h -o .moc/moc_form_main.cpp
- g++ -c -pipe -Wall -W -O2 -march=i386 -mcpu=i686 -g -DGLX_GLXEXT_LEGACY -fno-use-cxa-atexit -fno-exceptions
- -DQT_NO_DEBUG -I/usr/lib/qt-3.1/mkspecs/default -I. -I/usr/lib/qt-3.1/include -I.ui/ -I.moc/ -o .obj/moc_form_main.o .moc/moc_form_main.cpp
- g++ -o tutorial2 .obj/main.o .obj/form_main.o .obj/moc_form_main.o -L/usr/lib/qt-3.1/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
- [root@localhost DesignerTutorial2]# ./tutorial2
- [root@localhost DesignerTutorial2]# ls
- form_main.ui main.cpp Makefile tutorial2 tutorial2.pro
可以看到 tutorial2 ,這就是我們的可執(zhí)行程序,
運(yùn)行:
- [root@localhost DesignerTutorial2]# ./tutorial2
點(diǎn)擊按鈕看看,窗口關(guān)閉了,這不正是我們期望的嗎?
總結(jié):Qt Designer 教程調(diào)用退出的內(nèi)容介紹完了,這里我們學(xué)習(xí)了在Linux下 QT Designer 的基本使用方法,并且知道如何添加一個(gè)部件的信號。最后希望本文對你有所幫助!