Results 1 to 6 of 6

Thread: paintEvent on QGraphicsScene?

  1. #1
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question paintEvent on QGraphicsScene?

    Hi!

    I'm trying to make a program that draws a ellipse over an image when you click. I¡ll explain:

    I open an image with QGraphicsScene (subclassed). In the subclass I override the mouseReleaseEvent to collect the mouse clicks. I want to draw an ellipse when click the mouse in these coordinates. The coordinates are from the image and not from the window.

    This is the code, based on a example of QT:
    SubQGraphicsScene.cpp
    Qt Code:
    1. SubQGraphicsScene::SubQGraphicsScene(QObject *parent)
    2. : QGraphicsScene(parent)
    3. {
    4. // TODO Auto-generated constructor stub
    5. }
    6.  
    7. SubQGraphicsScene::~SubQGraphicsScene() {
    8. // TODO Auto-generated destructor stub
    9. }
    10.  
    11. void SubQGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
    12. {
    13. ultimo_x = event->scenePos().x();
    14. ultimo_y = event->scenePos().y();
    15. if (event->button() == Qt::LeftButton){
    16. cout << "Right button :" << ultimo_x << ", " << ultimo_y << endl;
    17. }else{
    18. cout << "Left button :" << ultimo_x << ", " << ultimo_y << endl;
    19. }
    20. update();
    21. this->update();
    22. }
    23.  
    24. void SubQGraphicsScene::paintEvent(QPaintEvent *event){
    25. cout << "PAINTER" << endl;
    26. //QGraphicsScene::paintEvent(event);
    27. //painter.begin(this);
    28. painter.setPen(QColor("#FF0000"));
    29. //painter.setBrush(QColor("#FFAAAA"));
    30. //Draw reference points
    31. painter.drawEllipse(ultimo_x,ultimo_y,20,20);
    32. //Draw points
    33. painter.end(); // close the painter
    34. }
    To copy to clipboard, switch view to plain text mode 
    SubQGraphicsScene.h:
    Qt Code:
    1. #ifndef SUBQGRAPHICSSCENE_H_
    2. #define SUBQGRAPHICSSCENE_H_
    3. #include <QGraphicsScene>
    4. #include <QPainter>
    5. #include <QtGui>
    6.  
    7. QT_BEGIN_NAMESPACE
    8. QT_END_NAMESPACE
    9.  
    10. class SubQGraphicsScene : public QGraphicsScene
    11. {
    12. Q_OBJECT
    13. public:
    14. SubQGraphicsScene(QObject *parent = 0);
    15. virtual ~SubQGraphicsScene();
    16.  
    17. private:
    18. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
    19. QPainter painter;
    20. int ultimo_x;
    21. int ultimo_y;
    22.  
    23. protected:
    24. void paintEvent(QPaintEvent *event);
    25. };
    26.  
    27. #endif /* SUBQGRAPHICSSCENE_H_ */
    To copy to clipboard, switch view to plain text mode 
    SvgView.cpp:
    Qt Code:
    1. SvgView::SvgView(QWidget *parent)
    2. : QGraphicsView(parent)
    3. , m_renderer(Native)
    4. , m_svgItem(0)
    5. , m_backgroundItem(0)
    6. , m_outlineItem(0)
    7. {
    8. mScene = new SubQGraphicsScene(this);
    9. setScene(mScene);
    10. setTransformationAnchor(AnchorUnderMouse);
    11. setDragMode(RubberBandDrag);
    12. setViewportUpdateMode(FullViewportUpdate);
    13.  
    14. // Prepare background check-board pattern
    15. QPixmap tilePixmap(64, 64);
    16. tilePixmap.fill(Qt::white);
    17. QPainter tilePainter(&tilePixmap);
    18. QColor color(220, 220, 220);
    19. tilePainter.fillRect(0, 0, 32, 32, color);
    20. tilePainter.fillRect(32, 32, 32, 32, color);
    21. tilePainter.end();
    22.  
    23. factor = 1.0;
    24.  
    25. setBackgroundBrush(tilePixmap);
    26. }
    27.  
    28. void SvgView::drawBackground(QPainter *p, const QRectF &)
    29. {
    30. p->save();
    31. p->resetTransform();
    32. p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
    33. p->restore();
    34. }
    35.  
    36. void SvgView::openFile(const QFile &file)
    37. {
    38. if (!file.exists())
    39. return;
    40. //QGraphicsScene *s = scene();
    41.  
    42. mScene->clear();
    43. resetTransform();
    44.  
    45. QPixmap pix( "C:\\i3s\\FotosINPUT\\prueba.JPG" );
    46. mScene->addPixmap(pix);
    47. mScene->update();
    48. }
    49.  
    50. void SvgView::setRenderer(RendererType type)
    51. {
    52. m_renderer = type;
    53.  
    54. if (m_renderer == OpenGL) {
    55. #ifndef QT_NO_OPENGL
    56. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    57. #endif
    58. } else {
    59. setViewport(new QWidget);
    60. }
    61. }
    62.  
    63. void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
    64. {
    65. #ifndef QT_NO_OPENGL
    66. setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
    67. #else
    68. Q_UNUSED(highQualityAntialiasing);
    69. #endif
    70. }
    71.  
    72. void SvgView::setViewBackground(bool enable)
    73. {
    74. if (!m_backgroundItem)
    75. return;
    76.  
    77. m_backgroundItem->setVisible(enable);
    78. }
    79.  
    80. void SvgView::setViewOutline(bool enable)
    81. {
    82. if (!m_outlineItem)
    83. return;
    84.  
    85. m_outlineItem->setVisible(enable);
    86. }
    87.  
    88. void SvgView::paintEvent(QPaintEvent *event)
    89. {
    90. if (m_renderer == Image) {
    91. if (m_image.size() != viewport()->size()) {
    92. m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
    93. }
    94.  
    95. QPainter imagePainter(&m_image);
    96. QGraphicsView::render(&imagePainter);
    97. imagePainter.end();
    98.  
    99. QPainter p(viewport());
    100. p.drawImage(0, 0, m_image);
    101.  
    102. } else {
    103. QGraphicsView::paintEvent(event);
    104. painter.begin(this);
    105. painter.setPen(QColor("#FF0000"));
    106. painter.drawEllipse(100,100,20,20);
    107. painter.end(); // close the painter
    108. }
    109. }
    110.  
    111. void SvgView::wheelEvent(QWheelEvent *event)
    112. {
    113. if(event->delta()>0){
    114. scale(1.25, 1.25);
    115. factor += 0.25; //Save the scale
    116. }else{
    117. scale(0.75, 0.75);
    118. factor -= 0.25; //Save the scale
    119. }
    120. event->accept();
    121. }
    To copy to clipboard, switch view to plain text mode 
    SvgView.h:
    Qt Code:
    1. class SvgView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. enum RendererType { Native, OpenGL, Image };
    7.  
    8. SvgView(QWidget *parent = 0);
    9.  
    10. void openFile(const QFile &file);
    11. void setRenderer(RendererType type = Native);
    12. void drawBackground(QPainter *p, const QRectF &rect);
    13. void mousePressEvent(QMouseEvent *event);
    14.  
    15. public slots:
    16. void setHighQualityAntialiasing(bool highQualityAntialiasing);
    17. void setViewBackground(bool enable);
    18. void setViewOutline(bool enable);
    19.  
    20. protected:
    21. void wheelEvent(QWheelEvent *event);
    22. void paintEvent(QPaintEvent *event);
    23.  
    24. private:
    25. RendererType m_renderer;
    26.  
    27. QGraphicsItem *m_svgItem;
    28. QGraphicsRectItem *m_backgroundItem;
    29. QGraphicsRectItem *m_outlineItem;
    30.  
    31. qreal factor;
    32. SubQGraphicsScene *mScene;
    33. QPainter painter;
    34. QImage m_image;
    35. };
    To copy to clipboard, switch view to plain text mode 

    It never draws an ellipse. SubQGraphicsScene:aintEvent never is called. SvgView:aintEvent is called, but not draw...

    Can anyone help me, please!

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: paintEvent on QGraphicsScene?

    QGraphicsScene doesn't have a paintEvent so it's not suprising your implementation of this method is not called.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: paintEvent on QGraphicsScene?

    Thanks!!

    So, how can I paint over the image?

    Another question, why QGraphicsView doesn't paint?

    Thanks a lot!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: paintEvent on QGraphicsScene?

    Quote Originally Posted by sergio87 View Post
    So, how can I paint over the image?
    You can paint on the viewport of the view, you can paint on the item or you can add an item to the scene. All depends on the effect you want to achieve.

    Another question, why QGraphicsView doesn't paint?
    You are probably opening the painter on the view and not its viewport.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: paintEvent on QGraphicsScene?

    Lots of thanks!

    Quote Originally Posted by wysota View Post
    You can paint on the viewport of the view, you can paint on the item or you can add an item to the scene. All depends on the effect you want to achieve.
    I'll try to add an item to the scene. Because in the future I would like to paint a lot of points (and that points will be in a file). I'll post the results!

    Thanks a lot!!

  6. #6
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: paintEvent on QGraphicsScene?

    I solved it:

    - FIRST solution:
    Qt Code:
    1. void SubQGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
    2. {
    3. ultimo_x = event->scenePos().x();
    4. ultimo_y = event->scenePos().y();
    5. if (event->button() == Qt::LeftButton){
    6. cout << "Right button :" << ultimo_x << ", " << ultimo_y << endl;
    7. }else{
    8. cout << "Left button :" << ultimo_x << ", " << ultimo_y << endl;
    9. }
    10. addEllipse(ultimo_x-10,ultimo_y-10,20,20);
    11. }
    To copy to clipboard, switch view to plain text mode 

    This code draw a ellipse when you click the mouse. This solution is very basic, but it works fine!

    - SECOND solution:
    For my interest, I subclassed QGraphicsItem and I paint in this class. Then I added it to the scene. It works perfect because I can draw a lot of things (ellipse, lines, etc). And is automatically scaled!

    If some has the same problem as me, there is the full code:
    svgView.cpp:
    Qt Code:
    1. #include "svgview.h"
    2. #include <QFile>
    3. #include <QWheelEvent>
    4. #include <QMouseEvent>
    5. #include <QGraphicsRectItem>
    6. #include <QGraphicsSvgItem>
    7. #include <QPaintEvent>
    8. #include <qmath.h>
    9. #include <iostream>
    10. using namespace std;
    11.  
    12. #ifndef QT_NO_OPENGL
    13. #include <QGLWidget>
    14. #endif
    15.  
    16. SvgView::SvgView(QWidget *parent)
    17. : QGraphicsView(parent)
    18. , m_renderer(Native)
    19. , m_svgItem(0)
    20. , m_backgroundItem(0)
    21. , m_outlineItem(0)
    22. {
    23. mScene = new SubQGraphicsScene(this);
    24. setScene(mScene);
    25. setTransformationAnchor(AnchorUnderMouse);
    26. setDragMode(RubberBandDrag);
    27. setViewportUpdateMode(FullViewportUpdate);
    28.  
    29. // Prepare background check-board pattern
    30. QPixmap tilePixmap(64, 64);
    31. tilePixmap.fill(Qt::white);
    32. QPainter tilePainter(&tilePixmap);
    33. QColor color(220, 220, 220);
    34. tilePainter.fillRect(0, 0, 32, 32, color);
    35. tilePainter.fillRect(32, 32, 32, 32, color);
    36. tilePainter.end();
    37.  
    38. factor = 1.0;
    39. setBackgroundBrush(tilePixmap);
    40. }
    41.  
    42. void SvgView::drawBackground(QPainter *p, const QRectF &)
    43. {
    44. p->save();
    45. p->resetTransform();
    46. p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
    47. p->restore();
    48. }
    49.  
    50. void SvgView::openFile(const QFile &file)
    51. {
    52. if (!file.exists())
    53. return;
    54. //QGraphicsScene *s = scene();
    55.  
    56. mScene->clear();
    57. resetTransform();
    58.  
    59. QPixmap pix( "C:\\i3s\\FotosINPUT\\prueba.JPG" );
    60. mScene->addPixmap(pix);
    61. mScene->update();
    62. fpPaint = new FingerPrintPainter(mScene->sceneRect());
    63. mScene->addItem(fpPaint);
    64. }
    65.  
    66. void SvgView::setRenderer(RendererType type)
    67. {
    68. m_renderer = type;
    69.  
    70. if (m_renderer == OpenGL) {
    71. #ifndef QT_NO_OPENGL
    72. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    73. #endif
    74. } else {
    75. setViewport(new QWidget);
    76. }
    77. }
    78.  
    79. void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
    80. {
    81. #ifndef QT_NO_OPENGL
    82. setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
    83. #else
    84. Q_UNUSED(highQualityAntialiasing);
    85. #endif
    86. }
    87.  
    88. void SvgView::wheelEvent(QWheelEvent *event)
    89. {
    90. if(event->delta()>0){
    91. scale(1.25, 1.25);
    92. factor += 0.25;
    93. }else{
    94. scale(0.75, 0.75);
    95. factor -= 0.25;
    96. }
    97. event->accept();
    98. }
    To copy to clipboard, switch view to plain text mode 
    svgView.h:
    Qt Code:
    1. #ifndef SVGVIEW_H
    2. #define SVGVIEW_H
    3.  
    4. #include <QGraphicsView>
    5. #include <QPainter>
    6. #include "SubQGraphicsScene.h"
    7. #include "FingerPrintPainter.h"
    8.  
    9. QT_BEGIN_NAMESPACE
    10. class QFile;
    11. QT_END_NAMESPACE
    12.  
    13. class SvgView : public QGraphicsView
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. enum RendererType { Native, OpenGL, Image };
    19.  
    20. SvgView(QWidget *parent = 0);
    21.  
    22. void openFile(const QFile &file);
    23. void setRenderer(RendererType type = Native);
    24. void drawBackground(QPainter *p, const QRectF &rect);
    25.  
    26. public slots:
    27. void setHighQualityAntialiasing(bool highQualityAntialiasing);
    28.  
    29. protected:
    30. void wheelEvent(QWheelEvent *event);
    31.  
    32. private:
    33. RendererType m_renderer;
    34.  
    35. QGraphicsItem *m_svgItem;
    36. QGraphicsRectItem *m_backgroundItem;
    37. QGraphicsRectItem *m_outlineItem;
    38.  
    39. qreal factor;
    40. SubQGraphicsScene *mScene;
    41. QPainter painter;
    42. QImage m_image;
    43.  
    44. FingerPrintPainter *fpPaint;
    45. };
    46. #endif // SVGVIEW_H
    To copy to clipboard, switch view to plain text mode 
    subQGraphicsScene.cpp:
    Qt Code:
    1. #include "SubQGraphicsScene.h"
    2. #include <QWheelEvent>
    3. #include <QMouseEvent>
    4. #include <QGraphicsSceneMouseEvent>
    5. #include <QGraphicsScene>
    6. #include <iostream>
    7. using namespace std;
    8. SubQGraphicsScene::SubQGraphicsScene(QObject *parent)
    9. : QGraphicsScene(parent)
    10. {
    11. // TODO Auto-generated constructor stub
    12. }
    13.  
    14. SubQGraphicsScene::~SubQGraphicsScene() {
    15. // TODO Auto-generated destructor stub
    16. }
    17.  
    18.  
    19. void SubQGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
    20. {
    21. ultimo_x = event->scenePos().x();
    22. ultimo_y = event->scenePos().y();
    23. if (event->button() == Qt::LeftButton){
    24. cout << "Botón derecho en :" << ultimo_x << ", " << ultimo_y << endl;
    25. }else{
    26. cout << "Botón izquierdo en :" << ultimo_x << ", " << ultimo_y << endl;
    27. }
    28. addEllipse(ultimo_x-10,ultimo_y-10,20,20);
    29. }
    To copy to clipboard, switch view to plain text mode 
    SubQGraphicsScene.h:
    Qt Code:
    1. #ifndef SUBQGRAPHICSSCENE_H_
    2. #define SUBQGRAPHICSSCENE_H_
    3. #include <QGraphicsScene>
    4. #include <QPainter>
    5. #include <QtGui>
    6.  
    7. QT_BEGIN_NAMESPACE
    8. QT_END_NAMESPACE
    9.  
    10. class SubQGraphicsScene : public QGraphicsScene
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. SubQGraphicsScene(QObject *parent = 0);
    16. virtual ~SubQGraphicsScene();
    17.  
    18. private:
    19. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
    20. QPainter painter;
    21. int ultimo_x;
    22. int ultimo_y;
    23.  
    24. };
    25.  
    26. #endif /* SUBQGRAPHICSSCENE_H_ */
    To copy to clipboard, switch view to plain text mode 
    SubQGraphicsItem.cpp:
    Qt Code:
    1. #include "SubQGraphicsItem.h"
    2. #include <QPainter>
    3. #include <time.h>
    4. #include <iostream>
    5. using namespace std;
    6. SubQGraphicsItem::SubQGraphicsItem(QRectF r) {
    7. // TODO Auto-generated constructor stub
    8. rect = r;
    9. }
    10.  
    11. SubQGraphicsItem::~SubQGraphicsItem() {
    12. // TODO Auto-generated destructor stub
    13. }
    14.  
    15. void SubQGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    16. {
    17. painter->setPen(Qt::NoPen);
    18. painter->setBrush(Qt::darkGray);
    19. srand ( time(NULL) );
    20. int mRandom;
    21. int mRandom2;
    22. for (int i=0; i<100; i++){
    23. mRandom = rand() % 3312 + 1;
    24. mRandom2 = rand() % 4416 + 1;
    25. painter->drawEllipse(mRandom, mRandom2, 20, 20);
    26. }
    27. }
    28.  
    29. QRectF SubQGraphicsItem::boundingRect() const
    30. {
    31. return rect;
    32. }
    To copy to clipboard, switch view to plain text mode 
    SubQGraphicsItem.h:
    Qt Code:
    1. #ifndef SubQGraphicsItem_H_
    2. #define SubQGraphicsItem_H_
    3. #include <QGraphicsItem>
    4.  
    5. class SubQGraphicsItem : public QGraphicsItem
    6. {
    7. public:
    8. SubQGraphicsItem(QRectF r);
    9. virtual ~SubQGraphicsItem();
    10. QRectF boundingRect() const;
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    12. private:
    13. QRectF rect;
    14. };
    15.  
    16. #endif /* SubQGraphicsItem_H_ */
    To copy to clipboard, switch view to plain text mode 

    This code draws 30 random ellipses.

    I hope it helps someone!!

    Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 24th May 2011, 17:29
  2. qt paintEvent
    By lzpmail in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2011, 13:13
  3. paintEvent help
    By seltra in forum Newbie
    Replies: 11
    Last Post: 18th September 2010, 19:24
  4. QGraphicsScene in paintEvent ?
    By PLan2010 in forum Newbie
    Replies: 5
    Last Post: 14th June 2010, 12:16
  5. Finish of PaintEvent
    By jimfan in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 07:50

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.