我正在製作一個程序,它將顯示一個目錄旁邊的幾個圖像。當我縮放圖像以適應窗口的高度(即-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());
}
更改爲光柵修復它。謝謝。 – jdrea 2011-05-30 21:43:20