1 / 26

Programming with Qt

Department of Computer and Information Science, School of Science, IUPUI. Programming with Qt. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Outline. The Qt Story The Layered X Programming APIs Implementing Cross-Platform GUI Libraries A Simple Example

keiran
Télécharger la présentation

Programming with Qt

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Programming with Qt Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. 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. GUI toolkits • Windows – MFC • 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, LCD) • Not based on API emulation (slow; different platforms require different API • Based on GUI emulation

  4. Signals and Slots • Various widgets • Panes – splitter • Tables – table • XML parsing – tagreader • Networking, sound, printing • 2D graphics – drawlinescanvasxform • OpenGL support – gear • OpenGL widgets, pixmaps – glpixmaps

  5. 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. The Layered X Programming APIs

  7. 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. “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 - disadvantage: 1. slower 2. awkward control flow 3. typically provide the lowest common denominator of functionality 4. difficult to inherit widgets and specialize them

  9. “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 2. emulated platform would be slower 3. unstable

  10. “Smarting-up” Components • GUI emulation - e.g. Qt - advantage: 1. fastest 2. changeable style 3. easy to inherit widgets and to redefine its behavior - disadvantage: 1. can’t 100% exact 2. codes has to rewritten if a new widget be created

  11. A Simple Example/* HelloWorld.cpp */ 1 #include <qapplication.h> 2 #include <qlabel.h> 3 4 int main(int argc, char **argv) { 5 6 QApplication myapp(argc, argv); 7 8 Qlabel *mylabel = new Qlabel(“Hello World”); 9 mylabel->resize(100, 200); 10 11 myapp.setMainWidget(mylabel); 12 mylabel->show(); 13 return myapp.exec(); 14 }

  12. 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(……); 2. XtAddCallback(quit, XmNactivateCallback, QuitCallback, NULL); 3. void QuitCallback(Widget w, XtPointer clientData, XtPointer callData){ exit(0); }

  13. 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. 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. Signals and Slots

  16. 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 approximately 50 microseconds on a SPARC2. )

  17. Signals and Slots(cont) 1 class PixmapRotator : public QWidget { 2 Q_OBJECT 3 public: 4 PixmapRotator(QWidget *parent=0, const char *name=0); 5 public slots: 6 void setAngle(int degrees); 7 signals: 8 void angleChanged(int); 9 private: 10 int ang; 11 }; 12 void PixmapRotator::setAngle( int degrees ) { 13 degrees = degrees % 360; // keep in range <-360, 360> 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. 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, slots and emit keywords are macros, so the compiler preprocessor changes or removes them. • How to do? 1. moc –o moc_file.moc moc_file.cpp moc_file.h 2. #include “moc_file.moc”

  19. 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. 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. 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. 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. 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. Other Features of Qt • The Qt Paint Engine - QPainter is highly optimized and contains several caching mechanisms to speed up drawing. Under X11, it caches GCs (graphics contexts), which often make it faster than native X11 programs. - QPainter contains all the functionality one would expect from a professional 2D graphics library. The coordinate system of a QPainter can be transformed using the standard 2D transformations (translate, scale, rotate and shear).

  25. 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 multi-platform applications. - Qt has platform independent support for the operating system dependent functions, such as time/date, files/directories and TCP/IP sockets.

  26. Acknowledgements • Plantinga, Harry. Calvin College • Trolltech Tutorials.

More Related