QGraphicsView - Linux 下缩放性能缓慢

发布于 2024-11-10 11:49:24 字数 2083 浏览 4 评论 0原文

我正在制作一个程序,它将显示目录中的一些图像并排。

当我缩放图像以适应窗口的高度时(即 - QGraphicsPixmapItem->scale(...)),它在 Windows 中运行得相当好,但在 Linux(Ubuntu 11.04)中运行速度慢得难以忍受。

如果图像未缩放,则两个系统上的性能相似。

我不确定这是否与每个操作系统缓存内存的方式有关,因为当我在 Linux 下运行程序时,使用的内存总是恒定的,约为 5mb,而在 Windows 下则接近 15-30mb,具体取决于图像已加载。

这是相关代码:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(scene);
    setCentralWidget(view);
    setWindowTitle(tr("ImgVw"));
    bestFit = true;

    view->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    view->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    view->setDragMode(QGraphicsView::ScrollHandDrag);
    view->setStyleSheet( "QGraphicsView { border-style: none; padding: 5px; background-color: #000; }" );   // set up custom style sheet

    // Get image files from folder
    QDir dir("test_img_folder");
    QStringList fileList = dir.entryList();
    fileList = fileList.filter(QRegExp(".*(\.jpg|\.jpeg|\.png)$"));

    // Create graphics item for each image
    int count = 0;
    foreach(QString file, fileList)
    {
    if (count >= 0)
    {
        QPixmap g(dir.absolutePath() + QString("/") + file);
        scene->addPixmap(g);
    }
    count++;
    if (count >= 5) break;
    }
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    int pos = 0;
    foreach(QGraphicsItem *item, scene->items(Qt::AscendingOrder))
    {
        double ratio = 1.0;
        QGraphicsPixmapItem *pixmapItem = (QGraphicsPixmapItem*) item;

        // Resize to fit to window
        if (bestFit) {
            double h = (double) (view->height()-10)/pixmapItem->pixmap().height();
            ratio = min(h, 1.0);
            pixmapItem->setScale(ratio);
        }

        // Position 5 pixels to the right of the previous image
        item->setPos(pos,0);
        pos += pixmapItem->pixmap().width()*ratio + 5;
    }

    // Resize scene to fit items
    scene->setSceneRect(scene->itemsBoundingRect());
}

I'm making a program that will display a few images from a directory beside each other.

When I scale the images to fit within the height of the window (ie - QGraphicsPixmapItem->scale(...)), it runs fairly well in windows, but runs unbearably slow in linux (Ubuntu 11.04).

If the images are not scaled, performance is similar on both systems.

I'm not sure if it has to do with the way each OS caches memory, since when I run the program under Linux, the memory used is always constant and around 5mb, when it's closer to 15-30mb under Windows depending on the images loaded.

Here is the related code:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(scene);
    setCentralWidget(view);
    setWindowTitle(tr("ImgVw"));
    bestFit = true;

    view->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    view->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
    view->setDragMode(QGraphicsView::ScrollHandDrag);
    view->setStyleSheet( "QGraphicsView { border-style: none; padding: 5px; background-color: #000; }" );   // set up custom style sheet

    // Get image files from folder
    QDir dir("test_img_folder");
    QStringList fileList = dir.entryList();
    fileList = fileList.filter(QRegExp(".*(\.jpg|\.jpeg|\.png)$"));

    // Create graphics item for each image
    int count = 0;
    foreach(QString file, fileList)
    {
    if (count >= 0)
    {
        QPixmap g(dir.absolutePath() + QString("/") + file);
        scene->addPixmap(g);
    }
    count++;
    if (count >= 5) break;
    }
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    int pos = 0;
    foreach(QGraphicsItem *item, scene->items(Qt::AscendingOrder))
    {
        double ratio = 1.0;
        QGraphicsPixmapItem *pixmapItem = (QGraphicsPixmapItem*) item;

        // Resize to fit to window
        if (bestFit) {
            double h = (double) (view->height()-10)/pixmapItem->pixmap().height();
            ratio = min(h, 1.0);
            pixmapItem->setScale(ratio);
        }

        // Position 5 pixels to the right of the previous image
        item->setPos(pos,0);
        pos += pixmapItem->pixmap().width()*ratio + 5;
    }

    // Resize scene to fit items
    scene->setSceneRect(scene->itemsBoundingRect());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2024-11-17 11:49:24

您可以尝试不同的图形系统,例如使用命令行开关 -graphicssystem raster|native|opengl 或将环境变量 QT_GRAPHICSSYSTEM 设置为“raster”等。

You could try different graphicssystems e.g. with the command line switch -graphicssystem raster|native|opengl or by setting the environment variable QT_GRAPHICSSYSTEM to "raster" etc.

似梦非梦 2024-11-17 11:49:24

根据我的经验,我同意尝试使用 QT_GRAPHICSSYSTEM 环境变量 hack。我花了一些时间开发具有高带宽回调的新实时 QT4 应用程序,才发现设置 QT_GRAPHICSSYSTEM = 'raster' 阻止了我的 RedHat Linux X11 系统不会占用 CPU 时间。所以我怀疑当 QT_GRAPHICSSYSTEM 未设置或设置为“native”时存在资源问题。

In my experience, I agree with trying the QT_GRAPHICSSYSTEM environment variable hack. It took me some time in development of a new real-time QT4 application, with high bandwidth callbacks, to discover that setting QT_GRAPHICSSYSTEM = 'raster', prevented my RedHat Linux X11 system from gobbling up CPU time. So I suspect there is a resource issue when QT_GRAPHICSSYSTEM is not set or set to 'native'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文