Zero Lee的专栏

Qt GUI Programming Basic

 

QT GUI PROGRAMMING BASIC

=====================================================================

CHAPTER1 NOTES

=======================

THIS CHAPTER CONTAINS 3 CASES USING QT TOOLKIT.

 

CASE 1: HELLO QT

 

PROGRAM CODES:

===============

1 #include <qapplication.h>  

2 #include <qlabel.h>

3

4 int main(int argc, char* argv[])

5 {

6  QApplication app(argc, argv);

7  QLabel* label = new QLabel(“Hello Qt!”, 0);

8  app.setMainWidget(label);

9  label->show();

10  return app.exec();

11 }

 

EXPLANATION:

=============

1,2 >> HEADER FILES CONTAIN THE DEFINITION OF QAPPLICATION AND QLABEL CLASSES.

6 >> CREATE QAPPLICATION OBJECT TO MANAGE THE RESOURCE OF THIS APPLICATION. AND PARAMETERS ARE COMMAND LINE ARGUMENTS. HOWEVER, HERE CONSTRUCTOR IS ONE OF IT.

7 >> CREATE ONE QLABEL WIDGET THAT DISPLAY “HELLO QT!”. IN QT TERM, A WIDGET IS A VISUAL ELEMENT IN A USER INTERFACE. FOR EXAMPLE, BUTTON, MENU, SCOLLBAR, AND FRAMES ARE WIDGETS. CERTAINLY, WIDGETS CAN CONTAIN OTHER WIDGETS. HERE, THIS SECOND ARGUMENT 0 MEANS THAT THE LABEL WIDGET IS ONE WINDOW IN ITS OWN RIGHT, NOT A CHILD WIDGET INSIDE ANOTHER WINDOW. CREATE ONE QLABEL OBJECT, SYNTAX:

QLabel ( const QString & text, QWidget * parent, const char * name = 0, WFlags f = 0 );

8 >> MAKE THE LABEL WIDGET THE APP’S MAIN WIDGET. WITHOUT A MAIN WIDGET, THE PROGRAM WOULD KEEP RUNNING IN THE BACKGROUND EVEN AFTER USER HAS CLOSED THE WINDOW.

 

9 >> MAKE LABEL VISIBLE. SHOW FUNCTION IS NOT PUBLIC ONE OF QLABEL. IT IS INHERITED FROM QWIDGET CLASS. WIDGETS ARE ALWAYS CREATED HIDDEN, SO THAT WE CAN CUSTOMIZE THEM BEFORE SHOWING THEM.

10 >> RETURN CONTROL OF APP TO QT. AT THIS POINT, THE PROGRAM ENTERS A KIND OF STAND-BY MODE(DEAD-LOOP), WHERE IT WAITS FOR USER ACTIONS SUCH AS MOUSE CLICKS AND KEY PRESSES.

 

RUN RESULT:

===========

 

 

=====================================================================

CASE 2: QUIT APPLICATION

 

PROGRAM CODES:

================

1 #include <qapplication.h>

2 #include <qpushbutton.h>

3

4 int main(int argc, char* argv[])

5 {

6   QApplication app(argc, argv);

7   QPushButton* button = new QPushButton(“Quit”, 0);

8   QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));

9   app.setMainWidget(button);

10 button->show();

11 return app.exec();

12 }

 

EXPLANATION:

=============

1,2 >> HEADER FILES CONTAIN THE DEFINITION OF QAPPLICATION AND QPUSHBUTTON CLASSES.

7 >> CREATE ONE QPUSHBUTTON OBJECT WHOSE CAPTION IS “QUIT”. HERE, CONSTRUCTOR SYNTAX IS:

QPushButton ( const QString & text, QWidget * parent, const char * name = 0 )

SECOND ARGUMENT 0 MEANING IS SAME AS QLABEL IN THE CASE1.

8 >> QT’S WIDGETS EMIT SIGNALS TO INDICATE THAT A USER ACTION OR A CHANGE OF STATE HAS OCCURRED. FOR INSTANCE, HERE QPUSHBUTTON EMITS A CLICKED() SIGNAL WHEN USER CLICKS THE BUTTON. A SIGNAL CAN BE CONNECTED TO A FUNCTION (CALLED A SLOT IN THAT CONTEXT), SO THAT WHEN THE SIGNAL IS EMITTED, THE SLOT IS AUTOMATICALLY EXECUTED. HERE, WE CONNECT THE BUTTON’S CLICKED() SIGNAL TO THE QAPPLICATION OBJECT’S QUIT() SLOT. SIGNAL() AND SLOT() MACORS ARE PART OF THE SYNTAX. MORE INFO ABOUT THEIR MECHNISM, PLEASE TURN TO QT ASSISTANT.

CONNET FUNCTION INVOKED SYNTAX:

bool connect (const QObject * sender, const char * signal, const QObject * receiver, const char* member)

 

RUN RESULT:

===========

 

 

=====================================================================

CASE 3: AGE APPLICATION

 

PROGRAM CODES:

================

1 #include <qapplication.h>

2 #include <qhbox.h>

3 #include <qslider.h>

4 #include <qspinbox.h>

5

6 int main(int argc, char* argv[])

7 {

8   QApplication app(argc, argv);

9   QHBox* hbox = new QHBox(0);

10 hbox->setCaption(“Enter Your Age”);

11 hbox->setMargin(50);

12 hbox->setSpacing(20);

13 

14 QSpinBox* spinBox = new QSpinBox(hbox);

15 QSlider* slider = new QSlider(Qt::Horizontal, hbox);

16 spinBox->setRange(0, 130);

17 slider->setRange(0, 130);

18 

19 QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));

20 QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));

21 spinBox->setValue(35);

22

23 app.setMainWidget(hbox);

24 hbox->show();

25

26 return app.exec();

27 }

 

EXPLANATION:

=============

1,2,3,4 >> HEADER FILES CONTAIN DEFINITION OF QAPPLICATION, QHBOX, QSLIDER AND QSPINBOX CLASSES.

8 >> CREATE ONE QHBOX OBJECT. THE QHBOX WIDGET PROVIDES HORIZONTAL GEOMETRY MANAGEMENT OFR ITS CHILD WIDGETS. SYNTAX:

QHBox ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 )

11, 12 >> USE SETMARGIN() TO ADD SPACE AROUND THE EDGES, AND USE SETSPACING() TO ADD SPACE BETWEEN THE WIDGETS(SPINBOX AND SLIDER).

 UNIT: PIXEL.

 

14, 15 >> CREATE CHILD WIDGETS IN QHBOX WIDGET. QSPINBOX AND QSLIDER.

WHEN CREATING QSPINBOX OBJECT, INVOKE THE FOLLOWING CONSTRUCOTR:

QSpinBox ( QWidget * parent = 0, const char * name = 0 );

WHEN CREATING QSLIDER OBJECT, INVOKE THE FOLLOWING CONSTRUCOTR:

QSlider ( Orientation orientation, QWidget * parent, const char * name = 0 );

QT::ORIENTATION: THIS TYPE IS ENUMUNATION TO SIGNIFY AN OBJECT’S ORIENTATION. TWO VALUES: QT::VERTICAL AND QT::HORIZONTAL.

19,20 >> THE TWO CONNECT() CALLS ENSURE THAT THE SPIN BOX AND THE SLIDER ARE SYNCHRONIZED SO THAT THEY ALWAYS SHOW THE SAME VALUE. WHENEVER THE VALUE OF ONE WIDGET CHANGES, ITS VALUECHANGED(INT) SIGNAL IS EMITTED, AND THE SETVALUE(INT) SLOT OF THE OTHER WIDGET IS CALLED WITH THE NEW VALUE.

 

21 >> WHEN THE SPLIN BOX VLAUE IS SET TO 35, THE QSPINBOX EMITS THE VALUECHANGED(INT) SIGNAL WITH AN INT ARGUMENT OF 35. THIS ARGUMENT IS PASSED TO THE QSLIDER’S SETVALUE(INT) SLOT, WHICH SETS THE SLIDER VALUE TO 35. THE SLIDER THEN EMITS THE VALUECHANGED(INT) SIGNAL, BECAUSE ITS OWN VLAUE CHANGED, TRIGGERING THE SPIN BOX’S SETVALUE(INT) SLOT. BUT AT THIS POINT, SETVALUE(INT) DOESN’T EMIT ANY SIGNAL, SINCE THE SPIN BOX IS ALREADY 35. THIS PREVENTS INFINITE RECURSION. THE FOLLOWING FIRURE EXPLANS THE SITUATION:

 

RUN RESULT:

============

 

CLASSES CHART:

===============

UNTILL NOW, THE CLASSES CHART:

 

 


RESOURCE LINKS:

================

1>> http://www.trolltech.com/

2>> http://www.qtcentre.org/

3>> http://doc.trolltech.com/4.2/examples.html#qt-tutorial

4>> http://www.qiliang.net/qt.html

posted on 2007-05-21 16:14 Zero Lee 阅读(2777) 评论(0)  编辑 收藏 引用 所属分类: CC++ Programming


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理