Daayus
Daayus

Reputation: 71

qt4 : call of update() on a single QGraphicsItem causes paint() on ALL QGraphicsItem

Summary

I use qt 4.8.7 under SUSE 64 bits

I have 2 QGraphicsItem with different refreshment rates. But when I call "update()" on one of them, "paint()" is called on both of them. So the real refreshment for both items rate is the highest common factor of the two refreshments.

I would like to have independent calls to paint() methods... I have no idea where does this issue come from and how to solve it (I tried to call QGraphicsItem::update(QRectF(//item_dimensions//))" instead of QGraphicsItem::update() but the problem is the same)

Simplified code

toto.hpp

class Toto : public QObject, QGraphicsItem
{

    Q_OBJECT

    public:
    Toto(QString name)
    {
        m_name = name;
    }

    void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget* = NULL)
    {
        QTextStream(stdout) << "paint : " << m_name << endl;
        //other stuff
    }

    public slots:
    void updateSlot()
    {
        QTextStream(stdout) << "\nupdate : " << m_name << endl;
        QGraphicsItem::update();
    }


    private:
    QString m_name;
}

main.cpp

Toto1 = new Toto("toto_1");
Toto2 = new Toto("toto_2");

QTimer *timer1 = new QTimer(500);
QTimer *timer2 = new QTimer(2000);

connect(timer1, SIGNAL(timeout()), toto1, SLOT(updateSlot()));
connect(timer2, SIGNAL(timeout()), toto2, SLOT(updateSlot()));

timer1->start();
timer2->start();

Expected output:

toto_1 update
toto_1 paint

toto_1 update
toto_1 paint

toto_1 update
toto_1 paint

toto_1 update
toto_1 paint

toto_2 update
toto_2 paint

toto_1 updated every 500ms, toto_2 updated every 2000ms

What I get:

toto_1 update
toto_1 paint
toto_2 paint

toto_1 update
toto_1 paint
toto_2 paint

toto_1 update
toto_1 paint
toto_2 paint

toto_1 update
toto_1 paint
toto_2 paint

toto_2 update
toto_1 paint
toto_2 paint

toto_1 and toto_2 both updated every 500ms

Thank you for your help !

Upvotes: 2

Views: 206

Answers (2)

Daayus
Daayus

Reputation: 71

I found a solution ! I simply added "setCacheMode(QGraphicsItem::DeviceCoordinateCache);", the default value used to be "QGraphicsItem::NoCache"

Upvotes: 1

Dariusz Scharsig
Dariusz Scharsig

Reputation: 1220

I am not sure if this might be the issue since I don't have all information but you could be a victim of a side effect that is documented in the QGraphicsItem::update() method, namely:

As a side effect of the item being repainted, other items that overlap the area rect may also be repainted.

This is a quote from the Qt4 documentation regarding QGraphicsItem::update() which you can check out yourself here.

Upvotes: 2

Related Questions