Hello,

I'd like to do a program, which allows user to "arrange" nodes (QGraphicsObjects) on screen (QGraphicsScene / QGraphicsView). Of course this is very simplified description.

Important part of "arranging" is selecting existing nodes by clicking on it. I decided to use QGraphicsItem::setSelected() method. But selected nodes are somehow automatically deselected (and redrawed) after clicking somewhere on screen.

I thought all logic of seleting/deselecting must be added by programmer, because I read documentation for these functions and can't see anythink about it. But maybe I overlooked something?

Thanks

------

Here is (again simplified) code I'm using, just with one added node by default. Probably most interesting line is 53, where I select the clicked node:

Qt Code:
  1. #include <QtGui>
  2. #include <QtGui/QGraphicsView>
  3. #include <QMouseEvent>
  4. #include <QRectF>
  5. #include <QPainterPath>
  6. #include <QPainter>
  7. #include <QStyleOptionGraphicsItem>
  8. #include <QWidget>
  9. #include <QColor>
  10. #include <QGraphicsScene>
  11. #include <QGraphicsObject>
  12. #include <QGraphicsSceneMouseEvent>
  13.  
  14. class Node : public QGraphicsObject
  15. {
  16. public:
  17. Node() { setFlag(ItemIsSelectable); }
  18.  
  19. QRectF boundingRect() const
  20. {
  21. qreal adjust = 2;
  22. return QRectF(-10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
  23. }
  24.  
  25. QPainterPath shape() const
  26. {
  27. path.addEllipse(-10, -10, 20, 20);
  28. return path;
  29. }
  30.  
  31. void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
  32. {
  33. QColor fill, stroke;
  34.  
  35. if( this->isSelected() ) {
  36. fill = Qt::yellow;
  37. stroke = Qt::gray;
  38. }
  39. else {
  40. fill = Qt::green;
  41. stroke = Qt::black;
  42. }
  43.  
  44. painter->setBrush(fill);
  45. painter->setPen(QPen( stroke, 0));
  46. painter->drawEllipse(-10, -10, 20, 20);
  47. }
  48.  
  49. protected:
  50. void mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
  51. {
  52. this->setSelected( !this->isSelected() );
  53. this->update( this->boundingRect() );
  54. event->accept();
  55. }
  56.  
  57. void mousePressEvent( QGraphicsSceneMouseEvent *event )
  58. {
  59. }
  60. };
  61.  
  62. class Playground : public QGraphicsView
  63. {
  64. public:
  65. Playground()
  66. {
  67. QGraphicsScene * scene = new QGraphicsScene( this );
  68. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
  69. scene->setSceneRect(0, 0, 200, 200);
  70. this->setScene(scene);
  71.  
  72. Node *newNode = new Node();
  73. scene->addItem( newNode );
  74. newNode->setPos( 100, 100 );
  75. }
  76.  
  77. protected:
  78.  
  79. void drawBackground( QPainter *painter, const QRectF &rect )
  80. {
  81. setInteractive ( true );
  82. QRectF sceneRect = this->sceneRect();
  83. painter->fillRect(rect.intersect(sceneRect), Qt::gray);
  84. painter->setBrush(Qt::NoBrush);
  85. painter->drawRect(sceneRect);
  86. }
  87. };
  88.  
  89.  
  90. int main(int argc, char **argv)
  91. {
  92. QApplication app(argc, argv);
  93.  
  94. Playground playground;
  95. playground.show();
  96.  
  97. return app.exec();
  98. }
To copy to clipboard, switch view to plain text mode