Results 1 to 6 of 6

Thread: How to iterate through GraphicsScene items using items()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to iterate through GraphicsScene items using items()

    I would like my QGraphicsScene to go through the GraphicItems that are on the screen, check some of the properties and then change the GraphicItems' color accordingly.

    In my QGraphicsScene class I have this code fragment. I expect it to only go through the foreach loop ONCE because there is only one item on the screen right now. But instead, it tries to go through twice. This is a problem because I get a segmentation fault when it tries to go through the loop the second time.

    Ideas??

    Qt Code:
    1. foreach(QGraphicsItem *item, items())
    2. {
    3. DiagramItem *zone = qgraphicsitem_cast<DiagramItem *>(item);
    4. myPenColor = zone->zoneColor;
    5. zone->setPen(myPenColor);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Here is my complete class for context:
    Qt Code:
    1. #include <QtGui>
    2. #include <sstream>
    3. #include <QGraphicsItem>
    4.  
    5. #include "scribblearea.h"
    6. #include "diagramitem.h"
    7.  
    8. QVector <QPoint> polygonPoints(4);
    9. QPolygon polygon(4);
    10.  
    11.  
    12. QPolygon ScribbleArea::getPoly()
    13. {
    14. return polygon;
    15. }
    16.  
    17. //! [0]
    18. ScribbleArea::ScribbleArea(QWidget *parent)
    19. : QGraphicsScene(parent)
    20. {
    21. //setAttribute(Qt::WA_StaticContents);
    22. modified = false;
    23. scribbling = false;
    24. myPenWidth = 1;
    25. myPenColor = Qt::blue;
    26. newZone = false;
    27. polyIndex = 0;
    28. zoneCount = 0;
    29.  
    30.  
    31. QPixmap pix;
    32. if (!pix.load( ":/images/iterislogo.jpg" ))
    33. {
    34. qWarning("Failed to load iterislogo.png");
    35. }
    36. else
    37. {
    38. QGraphicsPixmapItem *item = this->addPixmap(pix);
    39. item->setPos(0,500);
    40. }
    41. }
    42. //! [0]
    43.  
    44. //! [1]
    45. bool ScribbleArea::openImage(const QString &fileName)
    46. //! [1] //! [2]
    47. {
    48.  
    49. return true;
    50. }
    51. //! [2]
    52.  
    53. //! [3]
    54. bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat)
    55. //! [3] //! [4]
    56. {
    57.  
    58. return false;
    59. }
    60. //! [4]
    61.  
    62. //! [5]
    63. void ScribbleArea::setPenColor(const QColor &newColor)
    64. //! [5] //! [6]
    65. {
    66. myPenColor = newColor;
    67. }
    68. //! [6]
    69.  
    70. //! [7]
    71. void ScribbleArea::setPenWidth(int newWidth)
    72. //! [7] //! [8]
    73. {
    74. myPenWidth = newWidth;
    75. }
    76. //! [8]
    77.  
    78. void ScribbleArea::timesUp()
    79. {
    80.  
    81. //DiagramItem *zone = qgraphicsitem_cast<DiagramItem *>(all);
    82.  
    83. for (int i = 0; i < zoneCount; i++)
    84. {
    85. foreach(QGraphicsItem *item, items())
    86. {
    87. DiagramItem *zone = qgraphicsitem_cast<DiagramItem *>(item);
    88. myPenColor = zone->zoneColor;
    89. zone->setPen(myPenColor);
    90. }
    91. }
    92.  
    93.  
    94.  
    95. }
    96.  
    97. void ScribbleArea::activateImage()
    98. //! [9] //! [10]
    99. {
    100. foreach (QGraphicsItem *item, selectedItems()) {
    101. DiagramItem *zone = qgraphicsitem_cast<DiagramItem *>(item);
    102. myPenColor= Qt::red;
    103. zone->setPen(myPenColor);
    104.  
    105. zone->toggleState();
    106.  
    107. }
    108. }
    109.  
    110. void ScribbleArea::deactivateImage()
    111. //! [9] //! [10]
    112. {
    113. foreach (QGraphicsItem *item, selectedItems()) {
    114. DiagramItem *zone = qgraphicsitem_cast<DiagramItem *>(item);
    115. myPenColor = Qt::blue;
    116. zone->setPen(myPenColor);
    117.  
    118. zone->toggleState();
    119.  
    120. }
    121. }
    122.  
    123. //! [9]
    124. void ScribbleArea::clearImage()
    125. //! [9] //! [10]
    126. {
    127. foreach (QGraphicsItem *item, selectedItems()) {
    128. delete item;
    129. zoneCount--;
    130. }
    131. }
    132. //! [10]
    133. void ScribbleArea::drawZone()
    134. {
    135. newZone = true;
    136. }
    137.  
    138. //! [11]
    139. void ScribbleArea::mousePressEvent(QGraphicsSceneMouseEvent *event)
    140. //! [11] //! [12]
    141. {
    142. if (event->button() == Qt::LeftButton) {
    143. lastPoint = event->scenePos();
    144. scribbling = true;
    145.  
    146. if( newZone ) // If drawing a new zone
    147. {
    148. polygon.setPoint(polyIndex++, event->scenePos().x(), event->scenePos().y());
    149.  
    150. if( polyIndex > 3)
    151. {
    152. newZone = false;
    153.  
    154.  
    155. DiagramItem *item;
    156.  
    157. item = new DiagramItem(this);
    158. item->setPen(myPenColor);
    159. addItem(item);
    160.  
    161. QPointF details = event->scenePos(); // debug purposes only
    162.  
    163. polyIndex = 0;
    164. zoneCount++;
    165. }
    166. }
    167. }
    168. QGraphicsScene::mousePressEvent(event);
    169. }
    170.  
    171. void ScribbleArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    172. {
    173.  
    174. QGraphicsScene::mouseMoveEvent(event);
    175. }
    176.  
    177.  
    178. //! [14]
    179.  
    180. //! [15]
    181.  
    182. //! [18]
    183.  
    184. //! [19]
    185. void ScribbleArea::resizeImage(QImage *image, const QSize &newSize)
    186. //! [19] //! [20]
    187. {
    188. if (image->size() == newSize)
    189. return;
    190.  
    191. QImage newImage(newSize, QImage::Format_RGB32);
    192. newImage.fill(qRgb(255, 255, 255));
    193. QPainter painter(&newImage);
    194. painter.drawImage(QPoint(0, 0), *image);
    195. *image = newImage;
    196. }
    197. //! [20]
    198.  
    199.  
    200. //! [22]
    To copy to clipboard, switch view to plain text mode 
    Last edited by MercyYuen; 10th October 2012 at 00:55.

Similar Threads

  1. How to iterate through QListWidget items
    By zero-n in forum Newbie
    Replies: 6
    Last Post: 13th January 2012, 10:09
  2. Rendering items to graphicsscene
    By hema in forum Newbie
    Replies: 1
    Last Post: 20th July 2011, 07:43
  3. disabling multiple selection of items in graphicsscene/view
    By lightning2911 in forum Qt Programming
    Replies: 1
    Last Post: 23rd August 2010, 08:06
  4. Replies: 6
    Last Post: 27th March 2010, 05:42
  5. Iterate and get Checked QTable items??
    By darpan in forum Newbie
    Replies: 2
    Last Post: 10th May 2006, 18:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.