我正在制作一个程序,它将显示来自彼此并排的目录中的一些图像。
当我缩放图像以适应窗口的高度时(即 - 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());
}