QGraphicsView - Linux 下缩放性能缓慢
我正在制作一个程序,它将显示目录中的一些图像并排。
当我缩放图像以适应窗口的高度时(即 - 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());
}
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->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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试不同的图形系统,例如使用命令行开关 -graphicssystem raster|native|opengl 或将环境变量 QT_GRAPHICSSYSTEM 设置为“raster”等。
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.
根据我的经验,我同意尝试使用
QT_GRAPHICSSYSTEM
环境变量 hack。我花了一些时间开发具有高带宽回调的新实时QT4
应用程序,才发现设置QT_GRAPHICSSYSTEM = 'raster'
阻止了我的 RedHat LinuxX11
系统不会占用 CPU 时间。所以我怀疑当QT_GRAPHICSSYSTEM
未设置或设置为“native”时存在资源问题。In my experience, I agree with trying the
QT_GRAPHICSSYSTEM
environment variable hack. It took me some time in development of a new real-timeQT4
application, with high bandwidth callbacks, to discover that settingQT_GRAPHICSSYSTEM = 'raster'
, prevented my RedHat LinuxX11
system from gobbling up CPU time. So I suspect there is a resource issue whenQT_GRAPHICSSYSTEM
is not set or set to 'native'.