![](/img/trans.png)
[英]QGraphicsScene is calling QGraphicsItem paint event all the time
[英]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 毫秒更新一次
感謝您的幫助 !
我找到了解決方案! 我只是添加了“setCacheMode(QGraphicsItem::DeviceCoordinateCache);”,默認值曾經是“QGraphicsItem::NoCache”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.