PDA

View Full Version : How to use QGLWidget as the viewport for QGraphicsView?



tonnot
9th January 2012, 18:43
I'm trying to use a QGLWIDGET to test if my app. runs faster.
I see how the svgviewer example is faster when using it.
I have used a similar code :

glwidget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
G_view->setViewport(glwidget);

This is my paintEvent code for a QGraphicsView based widget, where I apply the transform and finally call to the internal paintevent


void A_Gview2D::paintEvent(QPaintEvent *event) {
QPainter painter (this->viewport());
.....
this->QGraphicsView::paintEvent(event);
}

I see a 'white' empty space and I have the errors:
A paint device can only be painted by one painter at a time.
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setWorldTransform: Painter not active
etc.

It seems that it is not so easy to apply. Any help ?
Thanks.

wysota
9th January 2012, 19:38
What is it exactly that you are trying to do? Why are you reimplementing paintEvent() here?

tonnot
9th January 2012, 20:11
Thanks wy.
Well , I have reimp. the paintEvent to have control over the paint events.
(I have a different way to paint things depend on some vars. Sometimes I use a pre-saved pixmap.)
Thanks.

wysota
9th January 2012, 20:35
What kind of control? The code you posted shouldn't be giving you the effects you have, at least not for one of recent versions of Qt. Are you sure this part is to blame?

tonnot
9th January 2012, 20:59
Excuse me , I dont understand the 'Are you sure... '

The errors are emitted when I try to create a painter.
When the program reaches and try to execute the next line is when the errors are showed.


QPainter painter (this->viewport());
QPainter::begin: A paint device can only be painted by one painter at a time.
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setWorldTransform: Painter not active
QPainter::worldTransform: Painter not active

Maybe I cant get the painter with this 'easy' code...
Is there another and correct way to have the painter in case of use a QGLwidget ?
Thanks.

wysota
9th January 2012, 21:22
The error manifests itself with the painter constructor call. But it doesn't mean the fault is actually there, it could be elsewhere. That's my point when asking whether you are sure the error is really caused by this statement.


#include <QtGui>
#include <QGLWidget>

class View : public QGraphicsView {
public:
View() {
setViewport(new QGLWidget);
}
protected:
void paintEvent(QPaintEvent *pe) {
// QPainter p(viewport());
QGraphicsView::paintEvent(pe);
}

};

int main(int argc, char **argv) {
QApplication app(argc, argv);
View v;
QGraphicsScene scene(QRect(0,0,400,300));
v.setScene(&scene);
scene.addRect(QRect(200,200, 100, 50));
v.show();
return app.exec();
}
Uncommenting painter construction indeed gives the error you describe.

However if you first call the base class implementation and then construct a painter, it works.


void paintEvent(QPaintEvent *pe) {
QGraphicsView::paintEvent(pe);
QPainter p(viewport());
}
If you do that, be sure to have a look at the "overpainting" Qt example otherwise you won't see much on your view apart a white background.