开发者

QGraphicsView - slow scale performance under Linux

开发者 https://www.devze.com 2023-03-08 21:48 出处:网络
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

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->setVerticalScro开发者_运维技巧llBarPolicy ( 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());
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号