gpt4 book ai didi

c++ - QGraphicsView 速度慢,有很多 QGraphicsPixmapItem

转载 作者:行者123 更新时间:2023-11-30 02:28:07 32 4
gpt4 key购买 nike

我编写了一个小代码来使用 QtCreator 测试 QGraphicsView 的功能。

代码非常简单,只是创建了一个继承自 QGraphicsView 的类,上面有一个 QGraphicsScene。用大量缩放到 100x100 的 QGraphicsPixmapItem(在本例中为 2000)填充场景,并将它们随机放置到场景中。

然后在自定义类中使用 QTimer 稍微移动场景中的所有元素。

(添加了第二个 QTimer 以查看每秒调用第一个 QTimer 的次数)。

它适用于几百个元素,但如果元素数量增加,性能会下降。

有人可以给我一些关于如何提高性能的提示吗?也许使用 QList 访问元素很慢......或者为这个简单的动画使用 QTimer 是一个非常糟糕的主意......或者必须在某处添加一些优化标志......也许忘记 QGraphicsView 并尝试 QtQuick 和 QML ...

最终的应用程序应该在屏幕上绘制大量图像,并使用 png 图像作为源为其中的一些图像设置动画。

测试.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test
TEMPLATE = app


SOURCES += main.cpp \
c_view.cpp

HEADERS += \
c_view.h

FORMS +=

RESOURCES += \
res.qrc

C_View.h

#ifndef C_VIEW_H
#define C_VIEW_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QTimer>

class C_View : public QGraphicsView
{
Q_OBJECT

public:
C_View();

QGraphicsScene *scene;

QGraphicsTextItem *label_fps;

QTimer timer;
QTimer timer_fps;
int interval=0;
int fps=0;
private slots:
void random_move();
void show_fps();
};

#endif // C_VIEW_H

C_View.cpp

#include "c_view.h"

C_View::C_View()
{
this->scene = new QGraphicsScene();
this->setScene(this->scene);

// Label to see how many times per seconds the random_move function gets called
this->label_fps=new QGraphicsTextItem();
this->label_fps->setDefaultTextColor(Qt::black);
this->label_fps->setFont(QFont("times",16));
this->label_fps->setPos(10,10);
this->scene->addItem(this->label_fps);

// Qtimer to enter random_move function
connect(&this->timer,SIGNAL(timeout()),this,SLOT(random_move()));
//this->interval=10; // 100 FPS?
this->interval=25; // 40 FPS?
//this->interval=50; // 20 FPS?
//this->interval=100; // 10 FPS?
this->timer.setInterval(this->interval);
this->timer.start();

// QTimer to update the FPS label
connect(&this->timer_fps,SIGNAL(timeout()),this,SLOT(show_fps()));
this->timer_fps.setInterval(1000); // Once a second
this->timer_fps.start();

}

// Funcion that moves a bit all the items of the scene
void C_View::random_move()
{
QList <QGraphicsItem*> l = this->items();
int ini=0;
for(int i=ini;i<l.size();i++)
{
l[i]->setPos(l[i]->x()+(rand()%3)-1,l[i]->y()+(rand()%3)-1);
}

this->fps++;
}

// Just show how many times random_move function gets call, since last time
void C_View::show_fps()
{
this->label_fps->setPlainText("FPS "+QString::number(this->fps));
this->label_fps->setZValue(1);
this->fps=0;
}

主要.cpp

#include <QApplication>

#include "c_view.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

C_View *view = new C_View();

// Fill the QGraphicsView with lots of images
for(int i=0;i<2000;i++)
{
QGraphicsPixmapItem *item=new QGraphicsPixmapItem();
item->setPixmap(QPixmap(":/images/p.png").scaled(100,100));
item->setPos(rand()%view->width(),rand()%view->height());
view->scene->addItem(item);
}

view->show();

return a.exec();
}

最佳答案

在我看来,放弃 Qt 的建议有点宿命论和不成熟。我们的应用程序使用具有数万个项目的 QGraphicsScene,并且性能良好。然而,它们中的大多数都不是图像,我们不会一次移动数千个。切换到其他东西可能最终会出现这种情况,但首先值得进行一些额外的实验。

分析器(如果您有的话)肯定能为您提供帮助。我们使用 Visual Studio 并且性能分析器在那里运行良好,但我不知道 Qt Creator 是否具有任何性能分析能力。您的示例应用程序非常简单,我看不出有什么明显的变化,但分析通常很能说明问题。

由于您要在场景中移动物体,请尝试 QGraphicsScene::setItemIndexMethod 和 QGraphicsScene::setBspTreeDepth 的选项。推荐的设置因您倾向于使用场景的方式而异。

在我看来,每秒移动 2000 个项目似乎很多。您说您的最终应用程序有很 multimap 像,但只有其中一些在移动。您预计其中 2000 人会搬家吗?

此外,您使用的是什么版本的 Qt?更高版本对要使用的渲染引擎做出更好的决定,我们的经验是这方面非常好。

关于c++ - QGraphicsView 速度慢,有很多 QGraphicsPixmapItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091078/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com