看到, 那个 button 中的标签已将变成 Exit 了. 我们这时侯还需要给这个 Exit Button 一个
signal(信号), 这样当您在 Exit 这个 Button 上 click 的时侯. QT 才知道如何去处理这个信
号. 我们按一下 F3(connect singnal slot)然後在那个 Exit Button 上面 Click 一下. 这时
侯我们就看到了 Edit Connection 的 Dialog 了. 在 Signal 中选择 clicked, 在 slot 中,先选
择 setFocus() 就好了. 这时侯选择 OK. 我们就算是完成了. 假如想看看这个小程式长什麽样子.
能够用 CTRL T来看 PreView. (see figure 1)
figure 1
首先在您的 $HOME 中建立一个 qt_program 的 Directory 出来. 这个 Directory 将会
作为我们存放文档的地方. 现在我们用 File -> Save 把这个文档存为 form1.ui 放在 $HOME/qt_program
的目录下.现在假如大家打开 form1.ui 来看一看. 会发现那是一堆有很多 的东西.所以我们需要用一个叫做 uic 的程式来把 .ui 文档转换成 QT 能够使用的 .cpp 和 .h 文档.
用下面的指令就能够生成我们需要的 .h 文档了
uic -o form1.h form1.ui
而生成 .cpp 文档则需要用以下的指令∶
uic -i form1.h -o form1.cpp form1.ui
这时侯,form1.h中就会看到一个标准的 QT 需要的 .h 文档
1. #ifndef FORM1_H
2. #define FORM1_H
3. #include
4. #include
5. class QVBoxLayout;
6. class QHBoxLayout;
7. class QGridLayout;
8. class QPushButton;
9. class Form1 : public QDialog
10. {
11. Q_OBJECT
12. public:
13. Form1( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
14. ~Form1();
15. QPushButton* PushButton1;
16. };
17. #endif // FORM1_H
1-2: 定义 FORM1.H 这个文档
3-4: 这里是我们需要用到的两个 .h 文档
5-7: 我们根本用不到, qt designer 自己产生的
8: QPushButton 需要用到这个 class
9-11: 我们的 form1 是 based 在 QDialog 上面的
12: 公开的 (能够在以後的程式中使用.用过 C 的人一定明白)
13: Form1的架构
14: 清除 Form1
15: 产生一个pushbutton (就是那个标有 exit 的 按钮
17: 结束对 FORM1.H 的定义
而 form1.cpp 文档如下:
1. #include "form1.h"
2. #include
3. #include
4. #include
5. #include
6. #include
7. /*
8. * Constructs a Form1 which is a child of 'parent', with the
9. * name 'name' and widget flags set to 'f'
10. *
11. * The dialog will by default be modeless, unless you set 'modal' to
12. * TRUE to construct a modal dialog.
13. */
14. Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )
15. : QDialog( parent, name, modal, fl )
16. {
17. if ( !name )
18. setName( "Form1" );
19. resize( 596, 480 );
20. setCaption( tr( "Form1" ) );
21. PushButton1 = new QPushButton( this, "PushButton1" );
22. PushButton1->setGeometry( QRect( 130, 160, 161, 71 ) );
23. PushButton1->setText( tr( "Exit" ) );
24. // signals and slots connections
25. connect( PushButton1, SIGNAL( clicked() ), PushButton1, SLOT( setFocus() ) );
26. }
27. /*
28. * Destroys the object and frees any allocated resources
29. */
30. Form1::~Form1()
31. {
32. // no need to delete child widgets, Qt does it all for us
33. }
1: 我们上面的定义文档
2: pushbutton所需要的 .h 文档
3-6: 我们根本用不到, qt designer 自己产生的
7-13: QT Designer 产生的注解
14-15: Form1 的结构
17-18: 假如Form1:Form1中没有 pass 一个名子过来.那麽就命名为 Form1
19: resize
20: 把显示出来的那个 Dialog 的名子定为 Form1, 也就是 window 中左上角的字
21: 做出一个新的button,名子为 PushButton1
22: 这里设定了 pushbutton 在这个 dialog 中的位置. Qrect(130, 160, 161, 71) 这里是说在一
个Dialog中,以左边最上面来算,位置是(0,0), 所以说,这里的130(横向)和 160 (纵向)就是说我
们从 (0,0)开始,往左边跑130,往下跑 160.这样我们就算出了pushbutton这个按钮画在那里了.後面的161,
71则是定义这个pushbutton到底要画多大,设定了长和高
23: 通过呼叫setText指令,我们能够在这个 button上面给入我们需要的文字.这里是 Exit
24: QT Designer 产生的注解
25: 上面就是处理当接收到 clicked 的信号(singal)以後,我们所作的事情(setFocus on PushButton1)
connect 这里是告诉程式连接一个信号,PushButton1, SIGNAL(clicked()),是说信号是由 PushButton1 发出,
发出的信号为 mouse clicked,PushButton1, SLOT(setFocus())表示信号发出以後,目标(Object)为 PushButton,
event 是 setFocus() 动作
Qt入门
最新推荐文章于 2025-03-28 20:38:30 发布