Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Qt Widget In-Depth

13,707 views

Published on

Everything you want to know about QWidget, but were afraid to ask. QWidget is the base-class for all Qt's user interface objects. This talk will take an in-depth look at how QWidget works internally and how it interacts with the native windowing system.

Presentation by Marius Bugge Monsen held during Qt Developer Days 2009.

http://qt.nokia.com/developer/learning/elearning

Published in: Technology
  • Be the first to comment

Qt Widget In-Depth

  1. 1. Qt Widgets In-Depth QWidget Under The Surface
  2. 2. About Me • Marius Bugge Monsen • Qt Developer • Qt Widgets Team Lead
  3. 3. • Widgets and Window Systems • Flags and Attributes • The Future of Qt Widgets
  4. 4. by extranoise on flickr Widgets and Window Systems
  5. 5. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  6. 6. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  7. 7. QWS SunView MiniGUI Metisse Rio Microwindows DM Fresco/Berlin 8 1/2 GEM ManaGeR HP Windows OOHG Xynth MicroXwin Intuition Y FBUI NeWS NeXT DPS Quartz Wayland Twin OPIE X
  8. 8. • X11 • Desktop Window Manager (MS Windows) • Quartz Compositor (Mac OS X) • QWS • S60 Window Manager
  9. 9. Window Surface
  10. 10. Surface Surface
  11. 11. Screen
  12. 12. Window System
  13. 13. Window System
  14. 14. Window System Qt Application IPC #include<QtGui> int main(int argc, char *argv[]) { QApplication a(argc,argv); QWidget w; w.show(); return a.exec(); }
  15. 15. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  16. 16. Surface Surface Surface
  17. 17. Surface Surface Surface
  18. 18. Widget Window Widget
  19. 19. Window
  20. 20. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  21. 21. Window System Paint Painting Code #include<QtGui> int main(int argc, char *argv[]) { QApplication a (argc,argv); QWidget w; w.show(); return a.exec(); } Update
  22. 22. Window System Backing Store Painting Code #include<QtGui> int main(int argc, char *argv[]) { QApplication a (argc,argv); QWidget w; w.show(); return a.exec(); }
  23. 23. Text Text
  24. 24. Text Text
  25. 25. • Client Side • Top-Level Window • Backing Store • Pixmap
  26. 26. • Server Side • Window • Pixmap
  27. 27. • Client Side • Server Side • Window • Window • Backing Store • Pixmap • Pixmap
  28. 28. void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QPoint &offset, int flags, QPainter *sharedPainter, QWidgetBackingStore *backingStore) { ... //actually send the paint event QPaintEvent e(toBePainted); QCoreApplication::sendSpontaneousEvent(q, &e); ... }
  29. 29. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  30. 30. • Spontaneous Events • Application Events
  31. 31. Input Event bool W::event(QEvent *e) { Any if (e->type() == t) foobar(); return false; }
  32. 32. Window System Socket Qt Event Dispatcher Event Handler bool W::event (QEvent *e) { if (e- >type() == Qt Event Loop
  33. 33. int QCoreApplication::exec() { ... QEventLoop eventLoop; self->d_func()->in_exec = true; self->d_func()->aboutToQuitEmitted = false; int returnCode = eventLoop.exec(); ... }
  34. 34. int QEventLoop::exec(ProcessEventsFlags flags) { ... try { while (!d->exit) processEvents(flags | WaitForMoreEvents | EventLoopExec); } catch (...) { ... --d->threadData->loopLevel; return d->returnCode; }
  35. 35. bool QEventLoop::processEvents(ProcessEventsFlags flags) { Q_D(QEventLoop); if (!d->threadData->eventDispatcher) return false; if (flags & DeferredDeletion) QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); return d->threadData->eventDispatcher->processEvents(flags); }
  36. 36. bool QEventDispatcherUNIX::processEvents (QEventLoop::ProcessEventsFlags flags) { ... // we are awake, broadcast it emit awake(); QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData); ... nevents = d->doSelect(flags, tm); ... }
  37. 37. int QEventDispatcherUNIXPrivate::doSelect( QEventLoop::ProcessEventsFlags flags, timeval *timeout) { ... // Process timers and socket notifiers - the common UNIX stuff ... nsel = q->select(highest + 1, &sn_vec[0].select_fds, &sn_vec[1].select_fds, &sn_vec[2].select_fds, timeout); ... }
  38. 38. int QEventDispatcherUNIX::select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, timeval *timeout) { return qt_safe_select(nfds, readfds, writefds, exceptfds, timeout); }
  39. 39. int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, const struct timeval *orig_timeout) { ... // loop and recalculate the timeout as needed int ret; forever { ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout); if (ret != -1 || errno != EINTR) return ret; // recalculate the timeout ... } }
  40. 40. • select() • poll status of file descriptors • blocks until timeout
  41. 41. X11 Socket XLib Queue Qt Event Dispatcher Event Handler bool W::event (QEvent *e) { if (e- >type() == Qt Event Loop
  42. 42. postEvent() Qt Event Queue Event Loop Event Handler #include<Q bool tGui> W::event int main(int (QEvent *e) argc, char { *argv[]) if (e- { >type() ==
  43. 43. sendEvent() Event Handler #include<Q bool tGui> W::event int main(int (QEvent *e) argc, char { *argv[]) if (e- { >type() ==
  44. 44. • Event Propagation
  45. 45. D A C B
  46. 46. D C A B
  47. 47. D C A B
  48. 48. D C A B
  49. 49. D C A B
  50. 50. bool QApplication::notify(QObject *receiver, QEvent *e) { ... bool res = false; if (!receiver->isWidgetType()) { res = d->notify_helper(receiver, e); } else switch (e->type()) { ... }
  51. 51. • Widgets Propagate Events
  52. 52. ... case QEvent::StatusTip: case QEvent::WhatsThisClicked: { QWidget *w = static_cast<QWidget *>(receiver); while (w) { res = d->notify_helper(w, e); if ((res && e->isAccepted()) || w->isWindow()) break; w = w->parentWidget(); } } break; ...
  53. 53. • Input Events Are Propagated
  54. 54. • Input Events are propagated if • event->isAccepted() == false • receiver->event(e) == false
  55. 55. bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event) { // send to all application event filters if (sendThroughApplicationEventFilters(receiver, event)) return true; // send to all receiver event filters if (sendThroughObjectEventFilters(receiver, event)) return true; // deliver the event return receiver->event(event); }
  56. 56. by Dan Queiroz on flickr Flags and Attributes
  57. 57. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  58. 58. • QWidget • QPaintDevice • QObject • QWindowSurface
  59. 59. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  60. 60. • Window Types • Widget • Window
  61. 61. • Dialog • Sheet (Mac) • Drawer (Mac) • Popup • ToolTip • SplashScreen • Desktop • SubWindow (MDI)
  62. 62. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  63. 63. • CustomizeWindowHint • MacWindowToolBarButtonHint • WindowTitleHint • BypassGraphicsProxyWidget • WindowSystemMenuHint • WindowShadeButtonHint • WindowMinimizeButtonHint • WindowStaysOnTopHint • WindowMaximizeButtonHint • WindowStaysOnBottomHint • WindowMinMaxButtonHint • WindowOkButtonHint (WinCE) • WindowCloseButtonHint • WindowCancelButtonHint (WinCE) • WindowContextHelpButtonHint
  64. 64. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  65. 65. • WindowState • WindowNoState • WindowMinimized • WindowMaximized • WindowFullScreen • WindowActive
  66. 66. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  67. 67. • Qt::Widget Attribute • 124 Attributes • setAttribute() • testAttribute()
  68. 68. • Qt::WA_AcceptDrops • QWidget::setAcceptDrops()
  69. 69. by robclimbing on flickr Tips and Tricks
  70. 70. • Qt::WA_StaticContents
  71. 71. Exposed Static Contents Exposed
  72. 72. • Qt::WA_NoSystemBackground
  73. 73. • Qt::WA_OpaquePaintEvent
  74. 74. • QWidget::autoFillBackground • Qt::WA_OpaquePaintEvent
  75. 75. • QWidget::scroll() • QWidget::autoFillBackground • Qt::WA_OpaquePaintEvent
  76. 76. Concealed Scrolled Exposed
  77. 77. • Qt::WA_TranslucentBackground
  78. 78. #include <QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPixmap skin("transparency.png"); QLabel widget; widget.setPixmap(skin); widget.setWindowFlags(Qt::Window |Qt::CustomizeWindowHint |Qt::FramelessWindowHint); widget.setAttribute(Qt::WA_TranslucentBackground); widget.resize(skin.size()); widget.show(); return app.exec(); }
  79. 79. by jeff_c on flickr The Future of Qt Widgets
  80. 80. • The story of two APIs ...
  81. 81. • QWidget • Widget Hierarchy • QGraphicsItem • Scene Graph
  82. 82. • QWidget • Alien Widgets • Graphics Effects • Disable Clipping ? • Disable Move Events ? • Transformations ?
  83. 83. • Is it possible ?
  84. 84. • Is it possible in Qt 4.x ?
  85. 85. Thank you! Questions?
  86. 86. Bonus Material
  87. 87. Qt Developer Days Window System
  88. 88. Window Surface Scene Graph IPC
  89. 89. QSharedMemory QGraphicsScene QTcpSocket
  90. 90. • Server • Window
  91. 91. • Server • Connections • Scene Graph
  92. 92. • Window • Surface • Geometry • Id
  93. 93. Server Client #include<QtGui> int main(int argc, char *argv[]) { QApplication a(argc,argv); QWidget w; w.show(); return a.exec(); }
  94. 94. Server Protocol Client ? #include<QtGui> int main(int argc, char *argv[]) { QApplication a(argc,argv); QWidget w; w.show(); return a.exec(); }
  95. 95. • Message • Request • Reply • Event
  96. 96. Request #include<QtGui> int main(int argc, char *argv[]) { QApplication a (argc,argv); QWidget w; w.show(); return a.exec(); } Response #include<QtGui> int main(int argc, char *argv[]) { QApplication a (argc,argv); QWidget w; w.show(); return a.exec(); }
  97. 97. Event bool W::event(QEvent *e) { if (e->type() == t) foobar(); return false; }
  98. 98. Lighthouse
  99. 99. • Research! • Qt Graphicssystem Interface • Makes Qt ports easy
  100. 100. • QGraphicsSystem • QGraphicsSystemScreen • QWindowSurface
  101. 101. • QGraphicsSystem • Window Surfaces • Server Communication
  102. 102. • QGraphicsSystemScreen • Screen Information • Depth • Resolution • Size
  103. 103. • QWindowSurface • Surface • Geometry • Id
  104. 104. Demo
  105. 105. Source Code • git://gitorious.org/+qt-developers/qt/lighthouse.git

×