Results 1 to 6 of 6

Thread: how to arrange item of QGraphicsScene inside QGraphicsView

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to arrange item of QGraphicsScene inside QGraphicsView

    hi
    I want to have 14 rectangles, 2 in each row inside every QGraphicsView. I tried to follow the 4000 chips example, it is nice. I tried to draw two rectangles with background images inside QGraphicsView. But, the problem I have is that I cannot arrange the position of the rectangles inside the view at all. If anyone worked with QGraphicsView could tell me how to add 14 medium sized rectangles inside the view...

    this is how I added the two rectangles inside the view:
    Qt Code:
    1. void MainWindow::populateScene()
    2. {
    3. scene = new QGraphicsScene;
    4.  
    5. QImage image("");//:/dolphin.png
    6.  
    7. // Populate scene
    8. int xx = 0;
    9. int nitems = 0;
    10. // for (int i = -11000; i < 11000; i += 110) {
    11. // ++xx;
    12. // int yy = 0;
    13. // for (int j = -7000; j < 7000; j += 70) {
    14. // ++yy;
    15. // qreal x = (i + 11000) / 22000.0;
    16. // qreal y = (j + 7000) / 14000.0;
    17.  
    18. QColor color(image.pixel(int(image.width() * 1), int(image.height() * 1)));
    19. // QGraphicsItem *item = new Chip(color, xx, yy);
    20. // item->setPos(QPointF(i, j));
    21.  
    22.  
    23. QGraphicsItem *item = new Chip(color, 0, 1);
    24. QGraphicsItem *item2 = new Chip(color, 0, 2);
    25.  
    26. item->setPos(QPointF(-300,0));
    27. item2->setPos(QPointF(100, 100));
    28. scene->addItem(item);
    29.  
    30. scene->addItem(item2);
    31. ++nitems;
    32. // }
    33. // }
    34. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /****************************************************************************
    2. **
    3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
    4. ** All rights reserved.
    5. ** Contact: Nokia Corporation (qt-info@nokia.com)
    6. **
    7. ** This file is part of the demonstration applications of the Qt Toolkit.
    8. **
    9. ** $QT_BEGIN_LICENSE:LGPL$
    10. ** GNU Lesser General Public License Usage
    11. ** This file may be used under the terms of the GNU Lesser General Public
    12. ** License version 2.1 as published by the Free Software Foundation and
    13. ** appearing in the file LICENSE.LGPL included in the packaging of this
    14. ** file. Please review the following information to ensure the GNU Lesser
    15. ** General Public License version 2.1 requirements will be met:
    16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    17. **
    18. ** In addition, as a special exception, Nokia gives you certain additional
    19. ** rights. These rights are described in the Nokia Qt LGPL Exception
    20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    21. **
    22. ** GNU General Public License Usage
    23. ** Alternatively, this file may be used under the terms of the GNU General
    24. ** Public License version 3.0 as published by the Free Software Foundation
    25. ** and appearing in the file LICENSE.GPL included in the packaging of this
    26. ** file. Please review the following information to ensure the GNU General
    27. ** Public License version 3.0 requirements will be met:
    28. ** http://www.gnu.org/copyleft/gpl.html.
    29. **
    30. ** Other Usage
    31. ** Alternatively, this file may be used in accordance with the terms and
    32. ** conditions contained in a signed written agreement between you and Nokia.
    33. **
    34. **
    35. **
    36. **
    37. **
    38. ** $QT_END_LICENSE$
    39. **
    40. ****************************************************************************/
    41.  
    42. #include "chip.h"
    43.  
    44. #include <QtGui>
    45.  
    46. Chip::Chip(const QColor &color, int x, int y)
    47. {
    48. this->x = x;
    49. this->y = y;
    50. this->color = color;
    51. setZValue((x + y) % 2);
    52.  
    53. setFlags(ItemIsSelectable | ItemIsMovable);
    54. setAcceptsHoverEvents(true);
    55. }
    56.  
    57. QRectF Chip::boundingRect() const
    58. {
    59. return QRectF(0, 0, 110, 70);
    60. }
    61.  
    62. QPainterPath Chip::shape() const
    63. {
    64. path.addRect(14, 14, 82, 42);
    65. return path;
    66. }
    67.  
    68. void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    69. {
    70. Q_UNUSED(widget);
    71.  
    72. QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
    73. if (option->state & QStyle::State_MouseOver)
    74. fillColor = fillColor.light(125);
    75.  
    76. const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    77. if (lod < 0.2) {
    78. if (lod < 0.125) {
    79. painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
    80. return;
    81. }
    82.  
    83. QBrush b = painter->brush();
    84. painter->setBrush(fillColor);
    85. painter->drawRect(13, 13, 97, 57);
    86. painter->setBrush(b);
    87. return;
    88. }
    89.  
    90. QPen oldPen = painter->pen();
    91. QPen pen = oldPen;
    92. int width = 0;
    93. if (option->state & QStyle::State_Selected)
    94. width += 2;
    95.  
    96. pen.setWidth(width);
    97. QBrush b = painter->brush();
    98. painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
    99.  
    100. // painter->drawRect(QRect(14, 14, 79, 39));
    101. // painter->drawRect(QRect(0, 0, 200, 200));
    102.  
    103. //painter->setBrush(b);
    104.  
    105. //QRect rect(14, 14, 79, 39);
    106. QRect rect(0, 0, 150, 150);
    107. painter->drawRect(rect);
    108.  
    109. QImage image = QImage("dolphin.png", 0);
    110. painter->drawImage(rect, image);
    111.  
    112. if (lod >= 1) {
    113. // painter->setPen(QPen(Qt::gray, 1));
    114. // painter->drawLine(15, 54, 94, 54);
    115. // painter->drawLine(94, 53, 94, 15);
    116. // painter->setPen(QPen(Qt::black, 0));
    117. }
    118.  
    119. // Draw text
    120. if (lod >= 2) {
    121. // QFont font("Times", 10);
    122. // font.setStyleStrategy(QFont::ForceOutline);
    123. // painter->setFont(font);
    124. // painter->save();
    125. // painter->scale(0.1, 0.1);
    126. // painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
    127. // painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
    128. // painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
    129. // painter->restore();
    130. }
    131.  
    132. // Draw lines
    133. QVarLengthArray<QLineF, 36> lines;
    134. if (lod >= 0.5) {
    135. // for (int i = 0; i <= 10; i += (lod > 0.5 ? 1 : 2)) {
    136. // lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
    137. // lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
    138. // }
    139. // for (int i = 0; i <= 6; i += (lod > 0.5 ? 1 : 2)) {
    140. // lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5));
    141. // lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5));
    142. // }
    143. }
    144. if (lod >= 0.4) {
    145. // const QLineF lineData[] = {
    146. // QLineF(25, 35, 35, 35),
    147. // QLineF(35, 30, 35, 40),
    148. // QLineF(35, 30, 45, 35),
    149. // QLineF(35, 40, 45, 35),
    150. // QLineF(45, 30, 45, 40),
    151. // QLineF(45, 35, 55, 35)
    152. // };
    153. // lines.append(lineData, 6);
    154. }
    155. // painter->drawLines(lines.data(), lines.size());
    156.  
    157. // Draw red ink
    158. if (stuff.size() > 1) {
    159. // QPen p = painter->pen();
    160. // painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    161. // painter->setBrush(Qt::NoBrush);
    162. // QPainterPath path;
    163. // path.moveTo(stuff.first());
    164. // for (int i = 1; i < stuff.size(); ++i)
    165. // path.lineTo(stuff.at(i));
    166. // painter->drawPath(path);
    167. // painter->setPen(p);
    168. }
    169. }
    170.  
    171. void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
    172. {
    173. QGraphicsItem::mousePressEvent(event);
    174. update();
    175. }
    176.  
    177. void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    178. {
    179. if (event->modifiers() & Qt::ShiftModifier) {
    180. stuff << event->pos();
    181. update();
    182. return;
    183. }
    184. QGraphicsItem::mouseMoveEvent(event);
    185. }
    186.  
    187. void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    188. {
    189. QGraphicsItem::mouseReleaseEvent(event);
    190. update();
    191. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 6th August 2012 at 20:56. Reason: changed [code] to [qtclass]

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to arrange item of QGraphicsScene inside QGraphicsView

    You haven't shown enough code to understand what you are doing wrong.

    Instead of hacking up the Chip code, you should start by simply inserting a QGraphicsRectItem into a scene and trying to move it to different positions:

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QMainWindow mainWin;
    6. mainWin.setCentralWidget( view );
    7.  
    8. pItem1->setRect( QRectF( 0, 0, 50, 50 ) );
    9. pItem1->setPos( 25, 25 );
    10. pItem1->setBrush( QBrush( Qt::red ) );
    11.  
    12. pItem2->setRect( QRectF( 0, 0, 75, 75 ) );
    13. pItem2->setPos( 50, 50 );
    14. pItem2->setPen( QPen( Qt::blue ) );
    15.  
    16. scene.addItem( pItem1 );
    17. scene.addItem( pItem2 );
    18.  
    19. view->setScene( &scene );
    20.  
    21. mainWin.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 


    Added after 24 minutes:


    Here is a little bit more complex an example. It shows how to use the z-values to put one object on top of another:

    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QMainWindow mainWin;
    6. mainWin.setCentralWidget( view );
    7.  
    8. for ( int i = 0; i < 7; ++i )
    9. {
    10. QGraphicsRectItem * pItem1 = new QGraphicsRectItem( 0, 0, 25, 25 );
    11. pItem1->setPos( 0, i * 50 );
    12. pItem1->setBrush( Qt::red );
    13. pItem1->setZValue( 10 );
    14.  
    15. QGraphicsRectItem * pItem2 = new QGraphicsRectItem( 0, 0, 25, 25 );
    16. pItem2->setPos( 100, i * 50 );
    17. pItem2->setBrush( Qt::yellow );
    18. pItem2->setZValue( 0 );
    19.  
    20. scene.addItem( pItem1 );
    21. scene.addItem( pItem2 );
    22.  
    23. // Connect item1 and item2 with a line
    24. QGraphicsItem * pLine = scene.addLine(
    25. QLineF( pItem1->mapToScene( pItem1->boundingRect().center() ),
    26. pItem2->mapToScene( pItem2->boundingRect().center() ) ) );
    27. pLine->setZValue( 5 );
    28. }
    29.  
    30. view->setScene( &scene );
    31.  
    32. mainWin.show();
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Notice that the z-value for the left (red) rect is higher than that of the right (yellow) rect, and the z-value for the line is between the two. So, as expected, the line is hidden under the left rectangle, while it is painted on top of the right rectangle.
    Last edited by d_stranz; 6th August 2012 at 22:48.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to arrange item of QGraphicsScene inside QGraphicsView

    thanks, that was so helpful! I created 13 rectangles, but, you know, there is a margin on the left n right side of the screen!
    I tried removing it, but it doesn't change anything. as it starts adding the rectangle from the center.

    Qt Code:
    1. QApplication app(argc, argv);
    2.  
    3. QMainWindow mainWin;
    4. mainWin.setCentralWidget( view );
    5.  
    6.  
    7. for ( int i = 0; i < 5; ++i )
    8. {
    9. QGraphicsRectItem * pItem1 = new QGraphicsRectItem( -1500, 5, 500, 200 );
    10. pItem1->setPos( 0, i * 205 );
    11. pItem1->setBrush( Qt::red );
    12. pItem1->setZValue( 10 );
    13.  
    14.  
    15. QGraphicsRectItem * pItem2 = new QGraphicsRectItem( -1500, 5, 500, 200 );
    16. pItem2->setPos( 505, i * 205);
    17. pItem2->setBrush( Qt::yellow );
    18. pItem2->setZValue( 0 );
    19.  
    20.  
    21. if( i < 3 )
    22. {
    23. QGraphicsRectItem * pItem3 = new QGraphicsRectItem( -1500, 5, 500, 200 );
    24. pItem3->setPos( 1010, i * 205);
    25. pItem3->setBrush( Qt::green);
    26. pItem3->setZValue( 0 );
    27.  
    28. scene.addItem( pItem3 );
    29. }
    30.  
    31. scene.addItem( pItem1 );
    32. scene.addItem( pItem2 );
    33.  
    34.  
    35. // Connect item1 and item2 with a line
    36.  
    37. // QGraphicsItem * pLine = scene.addLine(
    38. // QLineF( pItem1->mapToScene( pItem1->boundingRect().center() ),
    39. // pItem2->mapToScene( pItem2->boundingRect().center() ) ) );
    40. // pLine->setZValue( 5 );
    41. }
    42.  
    43. view->setScene( &scene );
    44.  
    45. mainWin.show();
    46. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    besides I tried to add a background image for each of the created rectangle, but it QGraphicsRectItem doesn not have the drawImage method. However, I added this to the Chip example to set the background images:
    Qt Code:
    1. QImage image("BildgeSystem.png");
    2. QRect rect(14, 14, 79, 39);
    3.  
    4. painter->drawImage (rect, image);
    To copy to clipboard, switch view to plain text mode 

    you know, I need to load a background image on each rectangle and add many svg switches n keys to it. I have never done that before, Could anyone explain this to me a bit. tnx : )

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to arrange item of QGraphicsScene inside QGraphicsView

    thanks, that was so helpful! I created 13 rectangles, but, you know, there is a margin on the left n right side of the screen!
    I tried removing it, but it doesn't change anything. as it starts adding the rectangle from the center.
    The view always centers the scene inside its viewport. You can change the location of the scene in the viewport by calling QGraphicsView::setAlignment() or by calling QGraphicsView::centerOn(). If you want the scene to grow or shrink according to the size of the view window, you can use QGraphicsView::fitInView() and pass the QGraphicsScene::sceneRect() as the argument.

    I will modify my example to show you how to draw an image into the rectangle. I am not at my office computer now, so I will do it later.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to arrange item of QGraphicsScene inside QGraphicsView

    you know, I need to add some gauge, keys, or. . in other words add some embeded widget n svg-s to each rectangle particular background png image. I would thank if anyone could introduce some tutorial or show some sample code which does so.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to arrange item of QGraphicsScene inside QGraphicsView

    OK, here's another example. I have derived a new class from QGraphicsPixmapItem. This class draws the pixmap, then draws more graphics (a rect and an ellipse) on top of the pixmap. If you need to use an SVG file instead of a pixmap, then derive a class from QGraphicsSvgItem instead. You will need the attached BMP file in order to build the application as I have posted it here.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include <QGraphicsRectItem>
    5. #include <QMainWindow>
    6. #include <QPixmap>
    7. #include <QGraphicsPixmapItem>
    8.  
    9. class ImageRectItem : public QGraphicsPixmapItem
    10. {
    11. public:
    12. ImageRectItem( QGraphicsItem * pParent = 0 )
    13. : QGraphicsPixmapItem( pParent )
    14. {}
    15.  
    16. ImageRectItem( const QPixmap & pixmap, QGraphicsItem * pParent = 0 )
    17. : QGraphicsPixmapItem( pixmap, pParent )
    18. {}
    19.  
    20. public:
    21. QRectF boundingRect() const
    22. {
    23. QRectF bounds( QGraphicsPixmapItem::boundingRect() );
    24. QRectF trans = bounds.translated( -bounds.center().x(), -bounds.center().y() );
    25. return trans;
    26. }
    27.  
    28. QPainterPath shape() const
    29. {
    30. path.addRect( boundingRect() );
    31. return path;
    32. }
    33.  
    34. void paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
    35. {
    36. // Call the base class to draw the pixmap
    37. QGraphicsPixmapItem::paint( painter, option, widget );
    38.  
    39. // Now draw some graphics on top of that
    40. QRectF rect = QGraphicsPixmapItem::boundingRect();
    41.  
    42. QPen pen( Qt::red );
    43. pen.setWidth( 4 );
    44. painter->setPen( pen );
    45. painter->drawRect( rect );
    46.  
    47. painter->setBrush( QBrush( Qt::blue ) );
    48. painter->drawEllipse( rect.center(), 10, 10 );
    49. }
    50. };
    51.  
    52. int main(int argc, char **argv)
    53. {
    54. QApplication app(argc, argv);
    55.  
    56. QPixmap pixMap( "MadMagazine.bmp" );
    57. QMainWindow mainWin;
    58. mainWin.setCentralWidget( view );
    59.  
    60. qreal x = 50.0;
    61. for ( int i = 0; i < 7; ++i )
    62. {
    63. ImageRectItem * pItem1 = new ImageRectItem( pixMap );
    64. pItem1->setPos( -pixMap.width() - x, i * ( 10 + pixMap.height() ) );
    65. pItem1->setZValue( 10 );
    66.  
    67. QGraphicsRectItem * pItem2 = new QGraphicsRectItem( 0, 0, 25, 25 );
    68. pItem2->setPos( pixMap.width() + x, i * pixMap.height() );
    69. pItem2->setBrush( Qt::yellow );
    70. pItem2->setZValue( 0 );
    71.  
    72. scene.addItem( pItem1 );
    73. scene.addItem( pItem2 );
    74.  
    75. // Connect item1 and item2 with a line
    76. QGraphicsItem * pLine = scene.addLine(
    77. QLineF( pItem1->mapToScene( pItem1->boundingRect().center() ),
    78. pItem2->mapToScene( pItem2->boundingRect().center() ) ) );
    79. pLine->setZValue( 5 );
    80.  
    81. x += 10.0;
    82. }
    83.  
    84. view->setScene( &scene );
    85.  
    86. mainWin.show();
    87. return app.exec();
    88. }
    To copy to clipboard, switch view to plain text mode 

    Bitmap file: MadMagazine.jpg
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. The following user says thank you to d_stranz for this useful post:

    saman_artorious (10th October 2013)

Similar Threads

  1. Replies: 7
    Last Post: 1st December 2011, 16:24
  2. QGraphicsView transparent over another QGraphicsView
    By evergreen in forum Qt Programming
    Replies: 0
    Last Post: 30th May 2011, 10:22
  3. Replies: 0
    Last Post: 10th January 2011, 09:42
  4. QGraphicsView
    By Maluko_Da_Tola in forum Newbie
    Replies: 11
    Last Post: 19th July 2010, 08:15
  5. QGraphicsView
    By hgedek in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2007, 09:16

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.