Download presentation
Presentation is loading. Please wait.
Published byBlaze Richardson Modified over 9 years ago
1
Dale Roberts Programming with Qt Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu Department of Computer and Information Science, School of Science, IUPUI
2
Dale Roberts Outline The Qt Story The Layered X Programming APIs Implementing Cross-Platform GUI Libraries A Simple Example Callback Function Signals and Slots Other Features of Qt
3
Dale Roberts GUI toolkits Windows – MFC (Microsoft Foundation Classes) Mac – MacOS Unix – many. Most popular: Motif (on which CDE is based). Specifies look and feel. (Difficult, error-prone, and not much fun.) QT: portable "framework" – also handles sending events to widgets Not based on layering (too slow to pass execution between layers) Not based on API emulation (slow; different platforms require different API) Based on GUI emulation (widgets are C++ classes)
4
Dale Roberts Signals and Slots Various widgets widgets Panes – splitter splitter Tables – table table XML parsing – tagreader tagreader Networking, sound, printing 2D graphics – drawlines canvas xform drawlinescanvasxformdrawlinescanvasxform OpenGL support – gear gear OpenGL widgets, pixmaps – glpixmaps glpixmaps
5
Dale Roberts The Qt Story The Qt toolkit is a multi-platform C++ GUI toolkit (class library) that has been developed over a 4 year period. The company Troll Tech AS was founded in 1994 to secure future development of Qt. Qt for X11 has a non-commercial license which grants any developer the right to use Qt to develop software for the free software community. Qt allows the user to choose between the Motif and the Windows look and feel.
6
Dale Roberts The Layered X Programming APIs
7
Dale Roberts Implementing Cross-Platform GUI Libraries Three typical approaches for developing a cross-platform product are to 1. “Dumb-down” the components 2. “Map” the components onto an existing API 3. “Smart-up” the components
8
Dale Roberts “Dumbing-down” Components API Layering - e.g. wxWindows - advantage: 1. easy to program 2. look-and-feel is 100% compatible with the native look-and-feel native look-and-feel - disadvantage: 1. slower 2. awkward control flow 3. typically provide the lowest common denominator of functionality of functionality 4. difficult to inherit widgets and specialize them
9
Dale Roberts “Mapping” Components API emulation - e.g. MainWin, Wind/U - advantage: 1. not necessary to emulate original platform - disadvantage: 1. too different for making API emulation very practical practical 2. emulated platform would be slower 3. unstable
10
Dale Roberts “Smarting-up” Components GUI emulation - e.g. Qt - advantage: 1. fastest 2. changeable style 3. easy to inherit widgets and to redefine its behavior behavior - disadvantage: 1. can’t 100% exact 2. codes has to rewritten if a new widget be created
11
Dale Roberts A Simple Example /* HelloWorld.cpp */ 1 #include 1 #include 2 #include 2 #include 3 4 int main(int argc, char **argv) { 4 int main(int argc, char **argv) { 5 6 QApplication myapp(argc, argv); 6 QApplication myapp(argc, argv); 7 8 Qlabel *mylabel = new Qlabel(“Hello World”); 8 Qlabel *mylabel = new Qlabel(“Hello World”); 9 mylabel->resize(100, 200); 9 mylabel->resize(100, 200);10 11 myapp.setMainWidget(mylabel); 12 mylabel->show(); 13 return myapp.exec(); 14 }
12
Dale Roberts Callback Function One of the most feared and hackish aspects of GUI programming has always been the dreaded callback-function. A register table for widget (e.g. Motif) - no type checking - example: 1. quit = XtVaCreateManagedWidget(……); 1. quit = XtVaCreateManagedWidget(……); 2. XtAddCallback(quit, XmNactivateCallback, 2. XtAddCallback(quit, XmNactivateCallback, QuitCallback, NULL); 3. void QuitCallback(Widget w, XtPointer 3. void QuitCallback(Widget w, XtPointer clientData, XtPointer callData){ clientData, XtPointer callData){ exit(0); exit(0); }
13
Dale Roberts Callback Function(cont) Virtual function(e.g. wxWindows) - too many classes need to inherit for all widgets - high dependence between GUI and kernel of application - would be slower in a inefficient vtable Macro(e.g. MFC 、 OWL) - message map was complicated and difficult to codeing - need additional preprocess, IDE or application builder
14
Dale Roberts Event Handling QT's new approach: signals and slots A widget sends out various signals Object methods can be declared as slots Compatible signals and slots can be connected or plugged together like a telephone switchboard (parameter types must match) Strict separation This strict separation between UI components and program elements lends itself to component-based programming Goal: separate UI from program logic
15
Dale Roberts Signals and Slots
16
Dale Roberts Signals and Slots (cont) advantage: - independent interface - type-safe - process transparence disadvantage: - not as fast as a direct function pointer call. ( A signal triggering a slot has been measured to ( A signal triggering a slot has been measured to approximately 50 microseconds on a SPARC2. ) approximately 50 microseconds on a SPARC2. )
17
Dale Roberts Signals and Slots(cont) 1 class PixmapRotator : public QWidget { 1 class PixmapRotator : public QWidget { 2 Q_OBJECT 2 Q_OBJECT 3 public: 3 public: 4 PixmapRotator(QWidget *parent=0, const char *name=0); 4 PixmapRotator(QWidget *parent=0, const char *name=0); 5 public slots: 5 public slots: 6 void setAngle(int degrees); 6 void setAngle(int degrees); 7 signals: 7 signals: 8 void angleChanged(int); 8 void angleChanged(int); 9 private: 9 private: 10 int ang; 11 }; 12 void PixmapRotator::setAngle( int degrees ) { 13 degrees = degrees % 360;// keep in range 13 degrees = degrees % 360;// keep in range 14 if(ang == degrees) 15 return; // actual state change? 16 ang = degrees; // a new angle 17 emit angleChanged(ang); // tell world... 18 } 19 QObject::connect(scrollBar, SIGNAL(valueChanged(int)), rotator, SLOT(setAngle(int)));
18
Dale Roberts Signals and Slots(cont) Qt meta object compiler (moc) - It parses C++ header files and generates C++ code necessary for Qt to handle signals and slots. The signals, necessary for Qt to handle signals and slots. The signals, slots and emit keywords are macros, so the compiler slots and emit keywords are macros, so the compiler preprocessor changes or removes them. preprocessor changes or removes them. How to do? 1. moc –o moc_file.moc moc_file.cpp moc_file.h 1. moc –o moc_file.moc moc_file.cpp moc_file.h 2. #include “moc_file.moc” 2. #include “moc_file.moc”
19
Dale Roberts Defining Signals and Slots New C++ syntax for defining signals and slots, added to public, private, etc. class myClass : public Qobject { Q_OBJECT//required macro, no semicolon …signals: void somethingHappened(); … public slots: void slotDoSomething(); … private slots: void slotDoSomethingInternal(); …};
20
Dale Roberts Events Signals: emit events declare as signals, otherwise normal member functions You don't implement them. Rather, you send them with the (new) keyword emit E.g. emit(sliderChanged(5)) Slots: receive and handle events Normal member functions declared as slots Connect: must connect signals to slots QObject::connect( mymenu, SIGNAL(activated(int)), myobject, SLOT(slotDoMenuFunction(int)) ); moc: meta object compiler (preprocessor) converts these new keywords to real C++
21
Dale Roberts Widgets Base class for all UI widgets Properties width, height, backgroundColor, font, mouseTracking, backgroundPixmap, etc. Slots repaint, show, hide, move, setGeometry, setMainWidget, etc. Signals: mouseMoveEvent, keyPressEvent, resizeEvent, paintEvent, enterEvent, leaveEvent, etc.
22
Dale Roberts Qt, a GUI toolkit Events processed with signals and slots signal generates an event, e.g., button push slot processes the event, e.g., pop up a file dialog box QPushButton * quitB = new QPushButton(“Quit”,...,...); connect (quitB, SIGNAL(clicked()), qApp, SLOT(quit()); qApp is a global variable, of type QApplication one QApplication per program defined first in main() main returns qApp.exec() SIGNAL and SLOT are macros, expanded by a meta-object compiler (moc) moc generates.cpp files from user-defined Qt subclasses
23
Dale Roberts Designing GUI’s What about Designing GUIs? Design decisions: who designs the GUI? What (if anything) do you need to know about app internals? Qt expertise, who has it? [remember, Java on the horizon] Implementation Lay out the GUI [draw it, sketch it] get the main widgets up and running, but not connected develop methods/events that are application specific develop commands, menus, buttons, etc. Compiling using moc, Qt classes, seeMakefile
24
Dale Roberts Other Features of Qt The Qt Paint Engine - QPainter is highly optimized and contains several caching mechanisms to speed up drawing. Under X11, caching mechanisms to speed up drawing. Under X11, it caches GCs (graphics contexts), which often make it it caches GCs (graphics contexts), which often make it faster than native X11 programs. faster than native X11 programs. - QPainter contains all the functionality one would expect from a professional 2D graphics library. The coordinate from a professional 2D graphics library. The coordinate system of a QPainter can be transformed using the system of a QPainter can be transformed using the standard 2D transformations (translate, scale, rotate and standard 2D transformations (translate, scale, rotate and shear). shear).
25
Dale Roberts Other Features of Qt(cont) Support Classes - Qt also contains a set of general purpose classes and a number of collection-classes to ease the development of number of collection-classes to ease the development of multi-platform applications. multi-platform applications. - Qt has platform independent support for the operating system dependent functions, such as time/date, system dependent functions, such as time/date, files/directories and TCP/IP sockets. files/directories and TCP/IP sockets.
26
Dale Roberts Acknowledgements Plantinga, Harry. Calvin College Trolltech Tutorials.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.