2001 12 Geometry Classes Under Qt Programming


KNOW HOW
QT
GETTING STARTED
WITH QT
Getting organised
To get us started this month we take a look at some 23. main() is much the
of the layout classes Qt has available for helping same as in the previous
create your interfaces. First, type in the following code we have looked at.
program and compile it: When you run the program
you get something like in
1 #include
Figure 1. As you can see,
2 #include
the four buttons are lined
3 #include
up vertically in a nice neat
4
fashion and they take up
5 class MyClass : public QVBox
equal space in the window.
6 {
Try editing out a button
7 public:
Figure 1: Four buttons
Welcome to part three
and recompiling, and you
equally spaced
8 MyClass();
will see that the space is
of our foray into the
9 ~MyClass();
accommodated cleanly for each button again. So
10
interesting world of
how does this magic work?
11 private:
12 QPushButton * bobButt; Well, if you look at line five, you can see we inherit
Qt application
13 QPushButton * fredButt;
QVBox. QVBox is a class for arranging widgets in a
development by Jono
14 QPushButton * frankButt;
vertical fashion and is very useful when you inherit
15 QPushButton * jimButt;
from it as it will automatically arrange child widgets
Bacon. This month we
16 };
into a vertical layout. In this example we added push
will take a long hard 17
buttons as child widgets, but let s also look at
18 MyClass::MyClass()
combining QVBox with a QHBox (for horizontal
look at geometry
19 {
layouts):
20 bobButt = new QPushButton( Bob , this);
classes for creating
21 fredButt = new QPushButton( Fred , this);
1 #include
interfaces and
22 frankButt = new QPushButton( Frank , this);
2 #include
23 jimButt = new QPushButton( Jim , this);
3 #include
examine how Qt deals
24 }
4 #include
25
with interaction with
5
26 MyClass::~MyClass()
6 class MyClass : public QVBox
our widgets
27 {
7 {
28 }
8 public:
29
9 MyClass();
30 int main( int argc, char **argv )
10 ~MyClass();
31 {
11
32 QApplication a( argc, argv );
12 private:
33
13 QHBox * hbox;
34 MyClass w;
14 QPushButton * bobButt;
35 a.setMainWidget( &w );
15 QPushButton * fredButt;
36 w.show();
16 QPushButton * frankButt;
37 return a.exec();
17 QPushButton * jimButt;
38 }
18 QPushButton * janButt;
19 QPushButton * aprilButt;
20 QPushButton * mayButt;
In this snippet of code we create four QPushButton
21
pointers on lines 12  15 (making sure to include
22 };
qpushbutton.h on line three). The actual
23
QPushButton objects are then created on lines 20 
34
LINUX MAGAZINE Issue 15 " 2001
KNOW HOW
(a graphical object on screen like a button) has a
24 MyClass::MyClass()
number of signals. A signal is a function that is
25 {
26 hbox = new QHBox(this); emitted when you do something with the widget. For
27 janButt = new QPushButton( Jan , hbox);
example, to see the signals that are available for
28 aprilButt = new QPushButton( April , hbox);
QPushButton s, we need to look at the QButton
29 mayButt = new QPushButton( May , hbox);
documentation (as QPushButton is a type of QButton
30 bobButt = new QPushButton( Bob , this);
and inherits it). We can see the following signals:
31 fredButt = new QPushButton( Fred , this);
32 frankButt = new QPushButton( Frank , this);
void pressed ()
33 jimButt = new QPushButton( Jim , this);
void released ()
34 }
void clicked ()
35
void toggled ( bool )
36 MyClass::~MyClass()
void stateChanged ( int )
37 {
38 }
39
So when a user clicks on a QPushButton, the clicked ()
40 int main( int argc, char **argv )
signal is emitted. We can then connect this signal to a
41 {
slot. A slot is just a normal method that can do whatever
42 QApplication a( argc, argv );
needed in response to the signal being emitted. So how
43
does this work, you ask? Well to explain, lets look at
44 MyClass w;
some code to get us started. You will need to use
45 a.setMainWidget( &w );
multiple files for this code. Type the following code in:
46 w.show();
47 return a.exec();
myclass.h:
48 }
1 #include
2 #include
In this example I firstly added hbox.h as an include
3 #include
file on line 2. I then created a pointer to a QHBox
4 #include
object on line 13, and
5
created the object on line
6 #ifndef MYCLASS_H
26. On lines 27  29 I
7 #define MYCLASS_H
created three more
8
A slot is
QPushButton objects (their
9 class MyClass : public QVBox
pointers being declared on
10 {
just a
lines 18  20), but instead 11 Q_OBJECT
normal
12
of setting the parent to
Figure 2: Now with three
13 public:
new push buttons
 this , I set it to  hbox
method
14 MyClass();
which is the name of the

15 ~MyClass(); that can do
QHBox object. By setting the parent to  hbox of a
16
widget, it is added to the layout manager specified in
whatever
17 public slots:
the parent and is organised. So when we create a
18 void slotJim();
needed in
QHBox object on line 26, it is then housing the new
19
three push buttons horizontally at the top of the
20 private: response to
vertical manager. This can all be seen in Figure 2.
21 QHBox * hbox;
the signal
Layout management is something integral to Qt
22 QPushButton * bobButt;
interface design. We will cover more on interface 23 QPushButton * fredButt;
being
24 QPushButton * frankButt;
design in the next issue.
emitted
25 QPushButton * jimButt;
26 QPushButton * janButt;
Connecting the pieces together
27 QPushButton * aprilButt;
OK, so we ve now come quite far. We have discussed
28 QPushButton * mayButt;
widgets, layout, parent/child relationships and written
29
a couple of small programs. This is all fine and dandy,
30 };
but our programs don t actually do anything yet. For
31
example when I click on a button, I want something

33 #endif
to happen. To do this there is a comprehensive
framework built right into Qt called the Signal/Slot
myclass.cpp:
framework. This is a system of connecting widgets to 1 #include
2 #include  myclass.h
functions so that when you do something some
3
functionality can be associated with it.
4 MyClass::MyClass()
The way signals and slots work is that each widget
35
Issue 15 " 2001 LINUX MAGAZINE
KNOW HOW
5 {
slotname() ) );
6 hbox = new QHBox(this);
moc
7 janButt = new QPushButton( Jan , hbox); We have the following code:
8 aprilButt = new QPushButton( April , hbox);
(Meta
9 mayButt = new QPushButton( May , hbox);
connect( jimButt, SIGNAL( clicked() ), this,
10 bobButt = new QPushButton( Bob , this);
SLOT( slotJim() ) );
Object
11 fredButt = new QPushButton( Fred , this);
Compiler)
12 frankButt = new QPushButton( Frank , this);
First of all we do not need the QObject:: prefix as we
13 jimButt = new QPushButton( Jim , this);

is a little
inherit QVBox which in turn inherits QObject down
14
the line. We can see that the jimButt object (the
tool that 15 connect( jimButt, SIGNAL( clicked() ), this,
button with  Jim written on it) is the object we are
SLOT( slotJim() ) );
converts
connecting a slot to. We are dealing with the
16 }
clicked() signal in this connection. We could of course
17
some of
18 MyClass::~MyClass() use any of the other signals, but clicked() is a good
the Qt
19 {
one to start with. We then connect this signal to the
20 }
slotJim() slot. We specify  this as the object whilst
signals and
21
MyClass has the slot definition.
22 void MyClass::slotJim()
slots
You may have seen some of the signals have a
23 {
parameter such as toggled( bool ). This signal is for
syntax into
24 QMessageBox::information( this,  Woohoo! ,
when the button is a toggle button and you want to
 slotJim() has been called!\n ,  Cancel );
regular
pass to the slot whether the button is toggled or not
25 }
as the parameter. To use signals that pass a
C++ code
parameter, your slot MUST accept the same
main.cpp:
parameter type. This may sound like a limitation but
1 #include
2 #include  myclass.h in practice it really isn t: this feature is due to Qt
3
being type safe which is a good thing. So for
4 int main( int argc, char **argv )
example you could have the following connection:
5 {
6 QApplication a( argc, argv );
connect( toggleButt, SIGNAL( toggled( bool ) ),
7
this, SLOT( slotIsToggled( bool ) ) );

8 MyClass w;
9 a.setMainWidget( &w );
You could then use the slotIsToggled( bool ) slot like
10 w.show();
this example:
11 return a.exec();
12 }
MyClass::slotIsToggled( bool state)
{
You will need to run the moc tool on the header file if
if( state == TRUE )
you are building this by hand. See the Qt documenta-
{
tion for details on this. Before we look at the code,
// do something
let s have a quick discussion of what moc actually is.
}
moc (Meta Object Compiler) is a little tool that
else
converts some of the Qt signals and slots syntax into
{
regular C++ code, and it also does some other nifty
// something else
little things. You can see this code for example in the
}
header file where you see  Q_OBJECT and public
}
slots:. The  Q_OBJECT code indicates you are using
the Qt object model (the signals/slots framework) in
this header file. Always put this at the top of any Wrapping things up
class that uses signals and slots. The  public slots: Well, in this tutorial we have looked at layout
part of the code indicates the following methods are managers, signals in widgets, signals and slots and a
slots that will be connected to signals. We have a few others things. We are well on the way now to
single slot slotJim() which is a method like any other writing more comprehensive Qt applications. Next
normal method. month we will build our first application based on
Now let s take a look at line 15. This line is where this knowledge and use Qt Designer to develop our
the actual connection between the signal and slot interfaces. Until then, I suggest you read through the
occurs. It is in this format: Qt documentation and have a play with the different
signals and methods available for widgets such as
QPushButton, QLabel etc. Have fun!
QObject::connect( object_that_emits_the_signal,
SLOT( signal() ), object_with_slot, SLOT(
36
LINUX MAGAZINE Issue 15 " 2001


Wyszukiwarka

Podobne podstrony:
2006 02 Qt ISO Maker–moja pierwsza aplikacja w Qt [Programowanie]
RAND CP22 2001 12
2001 12 Ośla łączka
2001 12 Gimp Workshop Image Processing
2007 02 Programowanie równoległe z Qt [Programowanie]
2001 12 Organize Package Management with Gnu Stow
2001 12 32
2001 12 Szkoła konstruktorów klasa II
C GUI QT programowanie
2001 12 Red Hat 7 2 on Test in the Linux Labs
2004 12 Piszemy grę w stylu Quake a [Programowanie]
2001 11 Programming with Qt
2009 12 Metaprogramowanie algorytmy wykonywane w czasie kompilacji [Programowanie C C ]
Programowanie notatka 10 09 12
12 Sekretów Błyskawicznego Zarabiania w Programie Partnerskim Chomikuj pl
12 Programowanie 5osi
Programowanie cwiczenia zjazd VII 18 12 2011
Doran New Advances in Geometric Algebra (2001) [sharethefiles com]

więcej podobnych podstron