qt怎么不用绘图事件画图片

由于有大量不会变的背景图片,如果每次定时器刷新都调用绘图事件(循环),感觉会浪费大量资源。请问有什么好的解决办法,使得大量的图片能像背景一样,不需要调用绘图事件就一直在界面上吗?

在 Qt 中,您可以使用 QGraphicsView、QGraphicsScene 和 QGraphicsItem 来绘制静态图像,而不是在绘图事件中绘制。

QGraphicsView 是一个用于显示 2D 图形的小部件,QGraphicsScene 是一个存储和管理图形图像的类,而 QGraphicsItem 是可以放置在场景中的图形图像的基类。

您可以将背景图片作为 QGraphicsPixmapItem 放入场景中,然后将场景设置为 QGraphicsView 的场景。这样,您就可以在不调用绘图事件的情况下,将背景图片一直显示在界面上了。

下面是一个简单的示例,展示了如何使用 QGraphicsView、QGraphicsScene 和 QGraphicsPixmapItem 在 Qt 中显示静态图像:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

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

    // 创建 QGraphicsView、QGraphicsScene 和 QGraphicsPixmapItem
    QGraphicsView view;
    QGraphicsScene scene;
    QGraphicsPixmapItem item(QPixmap("image.png"));

    // 将 QGraphicsPixmapItem 放入场景中
    scene.addItem(&item);

    // 将场景设置为 QGraphicsView 的场景
    view.setScene(&scene);

    // 显示 QGraphicsView
    view.show();

    return app.exec();
}