2

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

当我缩放图像以适应窗口的高度时(即 - 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());
}
4

2 回答 2

1

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

于 2011-05-30T14:47:35.263 回答
0

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

于 2012-06-11T07:54:51.997 回答