spud
23rd January 2008, 18:06
I'm having problems using the QGraphicsView to implement overlays. I simply would like to draw a semi-transparent box with fixed widget coordinates, containing some text. I thought drawForeground() would be the perfect solution. I get the widget coordinates from the viewport(ignoring the scrollbars) turn of the world matrix and paint.
The result looks good at first, but then something goes wrong. The viewport seems to cache the contents and doesn't always ask for an update when I scroll.
I've attached to screenshots and a full example. Does anybody have an idea how I can provoke an update, or if I am doing something wrong? I'm using Qt 4.3.2 on a windows machine
#include <QtGui>
QString text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"ullamco laboris nisi ut aliquip ex ea commodo consequat. "
"Duis aute irure dolor in reprehenderit in voluptate velit ...";
class GraphicsView : public QGraphicsView
{
public:
GraphicsView(QGraphicsScene* scene)
: QGraphicsView(scene)
{
}
QRectF overlayRect()const
{
return QRectF(viewport()->rect().width()-220, 20, 200, 200);
}
void drawForeground(QPainter *painter, const QRectF &rect)
{
painter->save();
painter->setWorldMatrixEnabled(false);
painter->setPen(QPen(Qt::darkGray, 2));
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(QColor(0,0,0,10));
painter->drawRoundRect(overlayRect());
painter->drawText(overlayRect().adjusted(20, 20, -20, -20), Qt::TextWordWrap , text);
painter->setWorldMatrixEnabled(true);
painter->restore();
QGraphicsView::drawForeground(painter, rect);
}
void scrollContentsBy(int dx, int dy)
{
// can I force an update here
QGraphicsView::scrollContentsBy( dx, dy);
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
scene.setSceneRect(0,0,2000,2000);
GraphicsView view(&scene);
view.show();
return app.exec();
}
P.S. The problem goes away if I pass the flag QGraphicsView::FullViewportUpdate, or use an OpenGL viewport, but I need a software rendering mode and would rather keep the updates to a minimum.
The result looks good at first, but then something goes wrong. The viewport seems to cache the contents and doesn't always ask for an update when I scroll.
I've attached to screenshots and a full example. Does anybody have an idea how I can provoke an update, or if I am doing something wrong? I'm using Qt 4.3.2 on a windows machine
#include <QtGui>
QString text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
"sed do eiusmod tempor incididunt ut labore et dolore magna "
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
"ullamco laboris nisi ut aliquip ex ea commodo consequat. "
"Duis aute irure dolor in reprehenderit in voluptate velit ...";
class GraphicsView : public QGraphicsView
{
public:
GraphicsView(QGraphicsScene* scene)
: QGraphicsView(scene)
{
}
QRectF overlayRect()const
{
return QRectF(viewport()->rect().width()-220, 20, 200, 200);
}
void drawForeground(QPainter *painter, const QRectF &rect)
{
painter->save();
painter->setWorldMatrixEnabled(false);
painter->setPen(QPen(Qt::darkGray, 2));
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(QColor(0,0,0,10));
painter->drawRoundRect(overlayRect());
painter->drawText(overlayRect().adjusted(20, 20, -20, -20), Qt::TextWordWrap , text);
painter->setWorldMatrixEnabled(true);
painter->restore();
QGraphicsView::drawForeground(painter, rect);
}
void scrollContentsBy(int dx, int dy)
{
// can I force an update here
QGraphicsView::scrollContentsBy( dx, dy);
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
scene.setSceneRect(0,0,2000,2000);
GraphicsView view(&scene);
view.show();
return app.exec();
}
P.S. The problem goes away if I pass the flag QGraphicsView::FullViewportUpdate, or use an OpenGL viewport, but I need a software rendering mode and would rather keep the updates to a minimum.