Search code examples
qtuser-interfaceqgraphicsviewqgraphicsscenepixmap

QGraphicsView - slow scale performance under Linux


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());
}

Solution

  • 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.