QGraphicsScene.itemAt()只返回零,整个场景很慢

时间:2015-09-16 14:11:00

标签: c++ performance qt qgraphicsscene

我正在尝试在QGraphicsScene中创建一个点阵。我有一个按钮,用随机数填充二维数组,然后我会在数组有0的每个位置上绘制一个像素。 现在,当我想再次生成矩阵时,我想检查每个像素和数组字段是否为空。如果像素为空且数组不是,我想设置一个像素。如果有像素但数组为空,我想删除像素。现在的问题是,函数itemAt()总是返回0,即使我可以清楚地看到存在的像素。 我的问题是什么?

//creating the scene in the constructor
QPainter MyPainter(this);
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);

//generating matrix
void MaReSX_ClickDummy::generate(void)
{
    QGraphicsItem *item;
    int x, y;
    for(x=0; x< 400; x++)
    {
        for(y=0; y<400; y++)
        {
            dataArray[x][y] = rand()%1001;
        }
    }

    for(x=0; x < 400; x++)
    {
        for(y=0; y<400; y++)
        {
            item = scene->itemAt(x, y, QTransform());//supposed to check whether there is already a pixel on that place but always returns zero
            if(dataArray[x][y] == 0 && item == 0)
                scene->addEllipse(x, y, 1, 1);
            //does not work
            else if(dataArray[x][y] != 0 && item != 0)
                scene->removeItem(item);
        }
    }
}

矩阵的生成也很慢。由于矩阵应该在以后显示实时数据,因此它应该尽可能快地运行。 (现在场景将超过400 * 400像素)。任何想法如何改进代码?

有人可以解释itemAt()的第三个参数应该做什么吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

400x400&#39;点阵&#39;高达16000个点,或最多2500个字符,这相当大。 QGraphicsScene设计用于处理少量大型形状,可能不是为处理这么多形状而设计的。以这种方式使用它来创建成千上万个相同的微小像素&#39;对象非常无穷无尽。

您可以创建一个400x400位图(QBitmap?),并设置您想要的各个像素吗?

答案 1 :(得分:0)

你应该使用QGraphicsPixmapItem而不是点数组!