【问题标题】:qt4 : call of update() on a single QGraphicsItem causes paint() on ALL QGraphicsItemqt4:在单个 QGraphicsItem 上调用 update() 会导致所有 QGraphicsItem 上的 paint()
【发布时间】:2019-11-08 02:23:05
【问题描述】:

总结

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

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

预期输出:

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

我得到了什么:

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 毫秒更新一次

感谢您的帮助!

【问题讨论】:

  • 你能告诉我你的 Toto::boundingRect() 的实现吗?

标签: c++ qt qt4 qgraphicsitem


【解决方案1】:

我不确定这是否是问题所在,因为我没有所有信息,但您可能是 QGraphicsItem::update() 方法中记录的副作用的受害者,即:

作为重绘项目的副作用,与区域 rect 重叠的其他项目也可能被重绘。

这是 Qt4 文档中关于 QGraphicsItem::update() 的引用,您可以自己查看 here

【讨论】:

  • 非常感谢您的回答。我的物品彼此相距 500 像素。即使我自己定义了一个小矩形,整个场景也会重新绘制。
  • 我已经尝试过了,但我没有得到相同的结果。我将降级到与您相同的版本,然后重试。
  • 好的,谢谢!如果对您有帮助,我可以为您提供更多信息
【解决方案2】:

我找到了解决办法! 我只是添加了“setCacheMode(QGraphicsItem::DeviceCoordinateCache);”,以前的默认值是“QGraphicsItem::NoCache”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多