[Interest] Qt4 --> Qt5: Forcing QGraphicsView::drawForeground() during QGraphicsView::mouseMoveEvent()

Sherif Ghali sherif.ghali.1 at gmail.com
Thu Apr 17 16:02:48 CEST 2014


With Qt4, QGraphicsView::drawForeground() is called with each
QGraphicsView::update(), even during mouse move events.
With Qt5, the update() requests are coalesced into a single repaint after
mouseReleaseEvent().
How can I reenable QGraphicsView::drawForeground() during mouse move events?

The brief code below shows the issue. With Qt4 the input point is painted
while it's dragged. With Qt5 it is not.
I'm using Qt 4.8.5 and Qt 5.2.1 on OS X 10.8.5.

Sherif

# points.pro
TEMPLATE = app
DEPENDPATH  += .
 INCLUDEPATH += .
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS += MyGraphicsView.h
SOURCES += main.cpp

// main.cpp
#include "MyGraphicsView.h"
#include <QApplication>
int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    MyGraphicsView view;
    view.show();
    return app.exec();
}

// MyGraphicsView.h
#include <QMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
class MyGraphicsView : public QGraphicsView
{
public:
    MyGraphicsView() : mouseIsPressed(false)
    {
        QGraphicsScene * scene = new QGraphicsScene;
        scene->setSceneRect(QRectF(QPointF(-4,-4), QSizeF(8,8)));
        setScene(scene);
        scale(1, -1);
        fitInView(scene->sceneRect() , Qt::KeepAspectRatio);
        setRenderHint(QPainter::Antialiasing);
    }
protected:
    void mousePressEvent(QMouseEvent * mouseEvent)
    {
        point = mapToScene(mouseEvent->pos());
        mouseIsPressed = true;
        update();
    }
    void mouseMoveEvent(QMouseEvent * mouseEvent)
    {
        point = mapToScene(mouseEvent->pos());
        update();
    }
    void mouseReleaseEvent(QMouseEvent * mouseEvent)
    {
        point = mapToScene(mouseEvent->pos());
        QGraphicsEllipseItem * ellipse = new
QGraphicsEllipseItem(draggedRect());
        ellipse->setPen(QPen(Qt::black, 0.1));
        scene()->addItem(ellipse);
        mouseIsPressed = false;
        update();
    }
    void drawForeground(QPainter * painter, const QRectF & /*rect*/)
    {
        if(mouseIsPressed) {
            painter->setPen(QPen(Qt::blue, 0.1));
            painter->drawEllipse(draggedRect());
        }
    }
private:
    QRectF draggedRect() {
        return QRectF(point.x()-0.1, point.y()-0.1, 0.2, 0.2);
    }
    QPointF point;
    bool mouseIsPressed;
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140417/db1151bb/attachment.html>


More information about the Interest mailing list