ディレクトリからいくつかの画像を並べて表示するプログラムを作成しています。
ウィンドウの高さに収まるように画像を拡大縮小すると(つまり、-QGraphicsPixmapItem-> scale(...))、Windowsではかなりうまく動作しますが、Linux(Ubuntu 11.04)では耐えられないほど遅くなります。
画像が拡大縮小されていない場合、パフォーマンスは両方のシステムで同様です。
Linuxでプログラムを実行すると、使用されるメモリは常に一定で、Windowsでは15〜30 MBに近い場合、画像によっては5 MB前後になるため、各OSがメモリをキャッシュする方法に関係があるかどうかはわかりません。ロードされました。
関連するコードは次のとおりです。
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());
}