2011-05-29 114 views
2

我正在制作一个程序,它将显示一个目录旁边的几个图像。当我缩放图像以适应窗口的高度(即-QGraphicsPixmapItem-> scale(...))时,它在Windows中运行得相当好,但在Linux(Ubuntu 11.04)中运行速度非常缓慢。QGraphicsView - Linux下的慢速缩放性能

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

我不确定它是否与每个操作系统缓存内存的方式有关,因为当我在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()); 
} 

回答

1

你可以尝试不同的graphicssystems例如使用命令行开关-graphicssystem raster | native | opengl或通过设置环境变量QT_GRAPHICSSYSTEM为“光栅”等。

+0

更改为光栅修复它。谢谢。 – jdrea 2011-05-30 21:43:20

0

以我的经验,我同意尝试使用QT_GRAPHICSSYSTEM环境变量hack。我花了一些时间来开发一个新的实时应用程序,带宽高的回调,发现设置QT_GRAPHICSSYSTEM = 'raster',阻止我的系统吞噬CPU时间。所以我怀疑当QT_GRAPHICSSYSTEM没有设置或设置为'本地'时有资源问题。