Qt Widget In-Depth

  • 11,286 views
Uploaded 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 …

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
11,286
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
328
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Qt Widgets In-Depth QWidget Under The Surface
  • 2. About Me • Marius Bugge Monsen • Qt Developer • Qt Widgets Team Lead
  • 3. • Widgets and Window Systems • Flags and Attributes • The Future of Qt Widgets
  • 4. by extranoise on flickr Widgets and Window Systems
  • 5. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  • 6. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  • 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. • X11 • Desktop Window Manager (MS Windows) • Quartz Compositor (Mac OS X) • QWS • S60 Window Manager
  • 9. Window Surface
  • 10. Surface Surface
  • 11. Screen
  • 12. Window System
  • 13. Window System
  • 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. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  • 16. Surface Surface Surface
  • 17. Surface Surface Surface
  • 18. Widget Window Widget
  • 19. Window
  • 20. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  • 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. 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. Text Text
  • 24. Text Text
  • 25. • Client Side • Top-Level Window • Backing Store • Pixmap
  • 26. • Server Side • Window • Pixmap
  • 27. • Client Side • Server Side • Window • Window • Backing Store • Pixmap • Pixmap
  • 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. • Widgets and Window Systems • Window Systems • Windows and Widgets • Updates and Painting • Events and Loops
  • 30. • Spontaneous Events • Application Events
  • 31. Input Event bool W::event(QEvent *e) { Any if (e->type() == t) foobar(); return false; }
  • 32. Window System Socket Qt Event Dispatcher Event Handler bool W::event (QEvent *e) { if (e- >type() == Qt Event Loop
  • 33. int QCoreApplication::exec() { ... QEventLoop eventLoop; self->d_func()->in_exec = true; self->d_func()->aboutToQuitEmitted = false; int returnCode = eventLoop.exec(); ... }
  • 34. int QEventLoop::exec(ProcessEventsFlags flags) { ... try { while (!d->exit) processEvents(flags | WaitForMoreEvents | EventLoopExec); } catch (...) { ... --d->threadData->loopLevel; return d->returnCode; }
  • 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. 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. 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. 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. 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. • select() • poll status of file descriptors • blocks until timeout
  • 41. X11 Socket XLib Queue Qt Event Dispatcher Event Handler bool W::event (QEvent *e) { if (e- >type() == Qt Event Loop
  • 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. sendEvent() Event Handler #include<Q bool tGui> W::event int main(int (QEvent *e) argc, char { *argv[]) if (e- { >type() ==
  • 44. • Event Propagation
  • 45. D A C B
  • 46. D C A B
  • 47. D C A B
  • 48. D C A B
  • 49. D C A B
  • 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. • Widgets Propagate Events
  • 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. • Input Events Are Propagated
  • 54. • Input Events are propagated if • event->isAccepted() == false • receiver->event(e) == false
  • 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. by Dan Queiroz on flickr Flags and Attributes
  • 57. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  • 58. • QWidget • QPaintDevice • QObject • QWindowSurface
  • 59. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  • 60. • Window Types • Widget • Window
  • 61. • Dialog • Sheet (Mac) • Drawer (Mac) • Popup • ToolTip • SplashScreen • Desktop • SubWindow (MDI)
  • 62. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  • 63. • CustomizeWindowHint • MacWindowToolBarButtonHint • WindowTitleHint • BypassGraphicsProxyWidget • WindowSystemMenuHint • WindowShadeButtonHint • WindowMinimizeButtonHint • WindowStaysOnTopHint • WindowMaximizeButtonHint • WindowStaysOnBottomHint • WindowMinMaxButtonHint • WindowOkButtonHint (WinCE) • WindowCloseButtonHint • WindowCancelButtonHint (WinCE) • WindowContextHelpButtonHint
  • 64. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  • 65. • WindowState • WindowNoState • WindowMinimized • WindowMaximized • WindowFullScreen • WindowActive
  • 66. • Flags and Attributes • Window Types • Window Hints • Widget States • Widget Attributes
  • 67. • Qt::Widget Attribute • 124 Attributes • setAttribute() • testAttribute()
  • 68. • Qt::WA_AcceptDrops • QWidget::setAcceptDrops()
  • 69. by robclimbing on flickr Tips and Tricks
  • 70. • Qt::WA_StaticContents
  • 71. Exposed Static Contents Exposed
  • 72. • Qt::WA_NoSystemBackground
  • 73. • Qt::WA_OpaquePaintEvent
  • 74. • QWidget::autoFillBackground • Qt::WA_OpaquePaintEvent
  • 75. • QWidget::scroll() • QWidget::autoFillBackground • Qt::WA_OpaquePaintEvent
  • 76. Concealed Scrolled Exposed
  • 77. • Qt::WA_TranslucentBackground
  • 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. by jeff_c on flickr The Future of Qt Widgets
  • 80. • The story of two APIs ...
  • 81. • QWidget • Widget Hierarchy • QGraphicsItem • Scene Graph
  • 82. • QWidget • Alien Widgets • Graphics Effects • Disable Clipping ? • Disable Move Events ? • Transformations ?
  • 83. • Is it possible ?
  • 84. • Is it possible in Qt 4.x ?
  • 85. Thank you! Questions?
  • 86. Bonus Material
  • 87. Qt Developer Days Window System
  • 88. Window Surface Scene Graph IPC
  • 89. QSharedMemory QGraphicsScene QTcpSocket
  • 90. • Server • Window
  • 91. • Server • Connections • Scene Graph
  • 92. • Window • Surface • Geometry • Id
  • 93. Server Client #include<QtGui> int main(int argc, char *argv[]) { QApplication a(argc,argv); QWidget w; w.show(); return a.exec(); }
  • 94. Server Protocol Client ? #include<QtGui> int main(int argc, char *argv[]) { QApplication a(argc,argv); QWidget w; w.show(); return a.exec(); }
  • 95. • Message • Request • Reply • Event
  • 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. Event bool W::event(QEvent *e) { if (e->type() == t) foobar(); return false; }
  • 98. Lighthouse
  • 99. • Research! • Qt Graphicssystem Interface • Makes Qt ports easy
  • 100. • QGraphicsSystem • QGraphicsSystemScreen • QWindowSurface
  • 101. • QGraphicsSystem • Window Surfaces • Server Communication
  • 102. • QGraphicsSystemScreen • Screen Information • Depth • Resolution • Size
  • 103. • QWindowSurface • Surface • Geometry • Id
  • 104. Demo
  • 105. Source Code • git://gitorious.org/+qt-developers/qt/lighthouse.git