QGraphicsScene.itemAt() 只有 returns 零,整个场景很慢
QGraphicsScene.itemAt() only returns zero and whole scene is slow
我正在尝试在 QGraphicsScene 中创建点阵。我有一个按钮,它用随机数填充一个二维数组,然后我会在数组有 0 的每个位置上绘制一个像素。
现在,当我想再次生成矩阵时,我想检查每个像素和数组字段是否为空。如果像素为空而数组不是,我想设置一个像素。如果有一个像素但数组为空,我想删除该像素。现在的问题是,函数 itemAt() 总是 returns 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() 的第三个参数应该做什么吗?
谢谢!
400x400 'dot matrix'最多16000个点,最多2500个字符,相当大。
QGraphicsScene 设计用于处理少量的大形状,可能不是为处理这么多形状而设计的。以这种方式使用它来创建数千个相同的微小 'pixel' 对象是非常低效的。
你能创建一个 400x400 位图(QBitmap 吗?),然后设置你想要的单个像素?
您应该使用 QGraphicsPixmapItem
而不是点阵!
我正在尝试在 QGraphicsScene 中创建点阵。我有一个按钮,它用随机数填充一个二维数组,然后我会在数组有 0 的每个位置上绘制一个像素。 现在,当我想再次生成矩阵时,我想检查每个像素和数组字段是否为空。如果像素为空而数组不是,我想设置一个像素。如果有一个像素但数组为空,我想删除该像素。现在的问题是,函数 itemAt() 总是 returns 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() 的第三个参数应该做什么吗?
谢谢!
400x400 'dot matrix'最多16000个点,最多2500个字符,相当大。 QGraphicsScene 设计用于处理少量的大形状,可能不是为处理这么多形状而设计的。以这种方式使用它来创建数千个相同的微小 'pixel' 对象是非常低效的。
你能创建一个 400x400 位图(QBitmap 吗?),然后设置你想要的单个像素?
您应该使用 QGraphicsPixmapItem
而不是点阵!