摘要
我在SUSE 64位下使用qt 4.8.7
我有2 QGraphicsItem和不同的茶点价格。但是,当我在其中一个上调用"update()“时,它们都会被调用”画图()“。因此,这两种茶点的实际进餐率是这两种茶点中最常见的因素。
我希望有独立调用来绘制()方法..。我不知道这个问题从何而来,也不知道如何解决(我试图调用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 painttoto_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 painttoto_1和toto_2每隔500 and更新一次
谢谢你的帮助!
发布于 2019-07-16 12:26:16
我不确定这是否是问题所在,因为我没有所有的信息,但您可能是QGraphicsItem::update()方法中记录的副作用的受害者,即:
作为正在重新绘制的项目的副作用,重叠区域的其他项目也可以重新绘制。
这是关于QGraphicsItem::update()的Qt4文档的引号,您可以自己查看它,这里。
发布于 2019-10-30 13:17:58
我找到了解决办法!我只是添加了"setCacheMode(QGraphicsItem::DeviceCoordinateCache);",,默认值以前是"QGraphicsItem::NoCache“
https://stackoverflow.com/questions/56767721
复制相似问题