May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

Graphics classes troubles…

 

1. I have an inherited class QGraphicsView and implemented paintEvent, why does debugger write: QPainter::begin: Paint device returned engine == 0, type: 1 ??
This is whole code:

  1. void GraphicsView::paintEvent(QPaintEvent *)
  2. {
  3.     QPainter painter(this);
  4.     painter.setBrush(QBrush(QColor(255,127,0)));
  5.     painter.drawRect(this->sceneRect());
  6. }

And I am not drawing outside from paintEvent method.
2. Why I have scollbars in my GraphicsView window ? How to remove them?

13 replies

May 1, 2011

leon.anavi leon.anavi
Mad Scientist
1045 posts
Peppy wrote:
2. Why I have scollbars in my GraphicsView window ? How to remove them?

QGraphicsView [doc.qt.nokia.com] inherits QAbstractScrollArea [doc.qt.nokia.com] so you can permanently disable the scrollbars by calling the following methods (please note the arguments):

  1. setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
  2. setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );

Best regards,
Leon

 Signature 

http://anavi.org/

May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

Thanks, and my first problem? What do you think?

May 1, 2011

leon.anavi leon.anavi
Mad Scientist
1045 posts
Peppy wrote:
Thanks, and my first problem? What do you think?

Honestly I did not understand what exactly is the problem. Btw what are you trying to achieve?

If you want just to set a background color it would be easier to use:

  1. setStyleSheet("background-color:rgb(255,127,0);");

QGraphicsView inherits QAbstractScrollArea, which inherits QFrame, which inherits QWidget so you should be able to call setStyleSheet :)

Cheers,
Leon

 Signature 

http://anavi.org/

May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

I have this code:

  1. QGraphicScene scene;
  2. scene.addRect(QRect(0,0,1000,700));
  3.  
  4. GraphicsView gv(&scene);
  5. gv.show();

And GraphicView class:
  1. class GraphicsView : public QGraphicsView
  2. {
  3. public:
  4.         explicit GraphicsView(QGraphicsScene* scene) : QGraphicsView(scene) { }
  5.  
  6. protected:
  7.             void paintEvent(QPaintEvent*);
  8. };
  9. void GraphicsView::paintEvent(QPaintEvent*)
  10. {
  11.     QPainter painter(this);
  12.     painter.setBrush(QBrush(QColor(255,127,0)));
  13.     painter.drawRect(this->sceneRect());
  14. }

Problem is, that debugger writes messages as I wrote above, and it doesn’t draw anything….

May 1, 2011

leon.anavi leon.anavi
Mad Scientist
1045 posts

Please check this thread at stackoverflow [stackoverflow.com] the proposed solution is to use viewport() [doc.qt.nokia.com] instead of this:

  1. QPainter painter(viewport());

Best regards,
Leon

 Signature 

http://anavi.org/

May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

Great! It works! Oh my God, it should be in docs…

May 1, 2011

leon.anavi leon.anavi
Mad Scientist
1045 posts

Cool :) So now both issues are solved thanks to the viewport!

 Signature 

http://anavi.org/

May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

Yes, but this important thing should be in docs, I think. :)

May 1, 2011

leon.anavi leon.anavi
Mad Scientist
1045 posts
Peppy wrote:
Yes, but this important thing should be in docs, I think. :)

You can add a wiki howto article about this issue here at Qt Developer network :)

 Signature 

http://anavi.org/

May 1, 2011

marcoB marcoB
Ant Farmer
167 posts

You can also reimplement QGraphicsScene::drawBackground [doc.qt.nokia.com]

or take a look also at backgroundBrush [doc.qt.nokia.com]

regards

May 1, 2011

Peppy Peppy
Hobby Entomologist
389 posts

When I used backgroundBrush (image via QBrush class) image was repeating on all axis. :)…

May 1, 2011

marcoB marcoB
Ant Farmer
167 posts

yes :)
you can reimplement drawbackground like this:

  1. void GraphicsView::drawBackground(QPainter *painter,
  2.                               const QRectF &f)
  3. {
  4.     Q_UNUSED(f);
  5.     QRectF rect;
  6.     rect=this->mapToScene(0,0, this->width(), this->height()).boundingRect();
  7.     painter->fillRect( rect,  QColor(255,127,0));
  8. }

May 1, 2011

ZapB ZapB
Robot Herder
1354 posts
Peppy wrote:
Yes, but this important thing should be in docs, I think. :)

QGV inherits QAbstractScrollArea and the docs there tell you all about the distinction between the viewport and the scroll area. Always a good idea to read the docs for the class(es) that the class of interest publically inherits too.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ [solved] Signals and Slots ... again      Creating 10bit images (like dpx) view application ››

You must log in to post a reply. Not a member yet? Register here!