簡體   English   中英

qt4:在單個 QGraphicsItem 上調用 update() 會導致所有 QGraphicsItem 上的paint()

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

概括

我在 SUSE 64 位下使用 qt 4.8.7

我有 2 個具有不同刷新率的 QGraphicsItem。 但是當我對其中一個調用“update()”時,它們兩個都調用了“paint()”。 所以兩個項目的真實刷新率是兩個刷新率的最大公因數。

我想獨立調用paint()方法......我不知道這個問題來自哪里以及如何解決它(我試圖調用QGraphicsItem::update(QRectF(//item_dimensions//))”而不是 QGraphicsItem::update() 但問題是一樣的)

簡化代碼

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;
}

主程序

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();

預期輸出:

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 每 500 毫秒更新一次,toto_2 每 2000 毫秒更新一次

我得到的:

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 和 toto_2 都每 500 毫秒更新一次

感謝您的幫助 !

我不確定這是否可能是問題,因為我沒有所有信息,但您可能是 QGraphicsItem::update() 方法中記錄的副作用的受害者,即:

作為重繪項目的副作用,與區域rect重疊的其他項目也可能被重繪。

這是 Qt4 文檔中關於 QGraphicsItem::update() 的引用,您可以在此處自行查看

我找到了解決方案! 我只是添加了“setCacheMode(QGraphicsItem::DeviceCoordinateCache);”,默認值曾經是“QGraphicsItem::NoCache”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM