[C++ GUI Programming with Qt4] chap8

4,593 views

Published on

2D Graphics. Description of 2D graphic operations in Qt4. In this Chapter, you can learn how to handle the graphic scenes, views, and items in the Qt program.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,593
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
132
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

[C++ GUI Programming with Qt4] chap8

  1. 1. Ch 8 2D Graphics Author: Tony
  2. 2. QPainter <ul><li>Point </li><ul><li>drawPoint
  3. 3. drawPoints </li></ul><li>Line </li><ul><li>drawLine
  4. 4. drawLines </li></ul><li>PolyLine </li><ul><li>drawPolyLine </li></ul></ul>
  5. 5. QPainter (cont.) <ul><li>drawPolygon
  6. 6. drawRect
  7. 7. drawRoundRect
  8. 8. drawEllipse </li></ul>
  9. 9. QPainter (cont.) <ul><li>drawArc
  10. 10. drawChord
  11. 11. drawPie </li></ul>
  12. 12. QPainter (cont.) <ul><li>DrawText
  13. 13. DrawPixmap
  14. 14. DrawPath </li></ul>
  15. 15. Three Main Painter Setting <ul><li>pen </li><ul><li>Drawing lines and shape outlines </li></ul><li>brush </li><ul><li>Filling geometric shape </li></ul><li>font </li><ul><li>Drawing text </li></ul></ul>
  16. 16. QPainterPath <ul><li>Specify the arbitrary vector shapes </li></ul>
  17. 17. Workflow of Painting Path <ul><li>Create Painter object and PainterPath object
  18. 18. Set render hint
  19. 19. Set painter path
  20. 20. Set painter setting for Painter object
  21. 21. Draw the path </li></ul>
  22. 22. Pen Style
  23. 23. Cap Style
  24. 24. Join Style
  25. 25. Brush Style
  26. 26. Gradient Fills <ul><li>Linear
  27. 27. Radial
  28. 28. Conical </li></ul>
  29. 30. Antialiasing
  30. 31. Antialiasing (cont.)
  31. 32. Window-Viewport Mechanism <ul><li>Make the drawing code independent of the size or resolution of the paint device
  32. 33. Convert the logical coordinates into physical coordinates </li><ul><li>painter.setWindow(-50, -50, 100, 100) </li></ul></ul>
  33. 34. World Transformation <ul><li>Multiple transformation </li><ul><li>Applied in the order in which they are given </li></ul><li>How many transformations? </li><ul><li>Translate
  34. 35. Scale
  35. 36. Rotate
  36. 37. Shear (ref. to Affine Transformations) </li></ul></ul>
  37. 38. World Transformation (cont.)
  38. 39. Reuse the Transformation <ul><li>QTransform </li></ul>
  39. 40. QTimer Class <ul><li>Provide repetitive and single-shot timers
  40. 41. How to use it? </li><ul><li>Create
  41. 42. Connect its timeout() signal to the slots
  42. 43. Call start() </li></ul><li>Result </li><ul><li>Emit the timeout() signal at constant intervals </li></ul></ul>
  43. 44. QTimer Class (cont.) <ul><li>An example, </li></ul>
  44. 45. QDateTime Class <ul><li>Provides date and time functions
  45. 46. For example, </li><ul><li>date()
  46. 47. time()
  47. 48. currentDateTime() </li></ul><li>Use of Gregorian and Julian Calendars </li></ul>
  48. 49. Rendering with QImage <ul><li>Rely on platform's native paint engine
  49. 50. Accuracy is more important than efficiency </li><ul><li>QImage </li></ul></ul>
  50. 51. How to paint by Qt's platform-independent graphics engine? <ul><li>Step by step </li><ul><li>Create a QPainter to draw on the image
  51. 52. Perform the drawing using the QPainter
  52. 53. QPainter object (image) -> widget </li></ul><li>Exception </li><ul><li>Font rendering </li></ul></ul>
  53. 54. How to paint by Qt's platform-independent graphics engine? (cont.) Initializes the painter's pen, background, and font
  54. 55. Composition Mode <ul><li>Default </li><ul><li>CompositionMode_SourceOver </li></ul></ul>
  55. 56. Graphics View <ul><li>QGraphicsScene </li><ul><li>Provides a surface for managing a large number of 2D graphical items </li></ul><li>QGraphicsItem </li><ul><li>The base class for all graphical items in a QGraphicsScene </li></ul><li>QGraphicsView </li><ul><li>provides a widget for displaying the contents of a QGraphicsScene </li></ul></ul>
  56. 57. Graphics View (cont.)
  57. 58. <ul>QGraphicsScene </ul><ul><li>Three layers </li><ul><li>Background, Item, Foreground </li></ul><li>QBrushes </li><ul><li>Background, Foreground </li></ul></ul>
  58. 59. Grouping Items <ul><li>Two ways </li><ul><li>Make an item a child of another item
  59. 60. QGraphicsItemGroup </li></ul></ul>
  60. 61. QGraphicsView
  61. 62. QgraphicsView's Render Engine <ul><li>Default </li><ul><li>Qt's build-in 2D paint engine </li></ul><li>Optional </li><ul><li>setViewport (OpenGL) </li></ul></ul>
  62. 63. Coordinate System <ul><li>Viewport </li><ul><li>Inside the QGraphicsView </li></ul><li>Scene </li><ul><li>Logical coordinate (positioning top-level items on the scene) </li></ul><li>Item </li><ul><li>Specific to each item </li></ul></ul>
  63. 64. Coordinate System (cont.) <ul><li>In practice, we only care about the scene coordinates and item coordinates </li></ul>
  64. 65. QGraphicsItem <ul><li>Every item has an (x,y) position and a z value </li></ul>
  65. 66. Q_DECLARE_TR_FUNCTIONS <ul><li>A class is not a QObject subclass
  66. 67. Want to use the tr function </li></ul>
  67. 68. itemChange <ul><li>ItemChange : Qvariant </li><ul><li>GraphicsItemChange change
  68. 69. const QVariant &value </li></ul><li>A handler for updating the item's change
  69. 70. Call update() to schedule a repaint
  70. 71. prepareGeometrychange() </li><ul><li>Call it before doing anything will affect the item's bounding rectangle
  71. 72. Ex: setText() </li></ul></ul>
  72. 73. ItemChange (cont.)
  73. 74. QGraphicsItem::boundingRect() <ul><li>boundingRect () : const QrectF </li><ul><li>pure virtual </li></ul><li>Called by QGraphicsView
  74. 75. whether the item needs to be re-drawn </li></ul>
  75. 76. QGraphicsItem::shape () <ul><li>shape() : QPainterPath </li><ul><li>virtual </li></ul><li>Called by QGraphicsView
  76. 77. Used to </li><ul><li>Collision detection
  77. 78. Hit test </li></ul></ul>
  78. 79. Select the Multiple Items <ul><li>Use the ctrl+click
  79. 80. QGraphicsView::RubberBandDrag </li><ul><li>Dragging a rubber band over all items you want to select </li></ul></ul>
  80. 81. Z-order of Views <ul><li>sendToBack()
  81. 82. bringToFront() </li></ul>
  82. 83. Change the Color <ul><li>Qt's build-in dialog </li><ul><li>QColorDialog::getcolor() </li></ul><li>When the link is changed, call update()
  83. 84. QColor::name() </li><ul><li>Get the color string in #RRGGBB format </li></ul></ul>
  84. 85. Zoom Factor <ul><li>QstyleOptionGraphicsItem class </li><ul><li>A floating-point value represents as zoom factor
  85. 86. Describe the parameters needed to draw a QGraphicsItem </li></ul></ul>
  86. 87. Animation Effect <ul><li>QGraphicsItemAnimations
  87. 88. QTimeLine </li><ul><li>Reimplement the QObject::timeEvent() </li></ul></ul>
  88. 89. Printing <ul><li>QPrinter::setPrintProgram()
  89. 90. PDF format </li><ul><li>setOutputFormat(QPrinter::PdfFormat) </li></ul><li>Print the entire scene </li><ul><li>QGraphicsView::render() </li></ul><li>Multiple pages printing </li><ul><li>Paint one page
  90. 91. call newPage() to advance to the next page </li></ul><li>Print the range </li><ul><li>fromPage() - toPage() </li></ul></ul>
  91. 92. Printing (cont.)

×