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>
public:
View() {
}
protected:
// QPainter p(viewport());
}
};
int main(int argc, char **argv) {
View v;
v.setScene(&scene);
scene.
addRect(QRect(200,
200,
100,
50));
v.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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());
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks