QLabel与QGraphicsView的表现

Ale*_*lex 9 performance qt qgraphicsview qlabel

我正在学习QT,我对QLabel和QGraphics视图在平移时的性能差异感到困惑.

我将一个巨大的36Mpixels(D800)jpeg文件读入QLabel或QGraphics对象,并尝试使用QLabel/Graphics拖动全尺寸图像.令人惊讶的是,QLabel提供了非常平滑的运动,而QGRaphicsView平移是生涩的.

简化的QGraphicsView代码是:

QApplication::setGraphicsSystem("raster");    
...
QGraphicsView  view();
view.setDragMode(QGraphicsView::ScrollHandDrag);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setFrameStyle(QFrame::NoFrame);
view.showFullScreen();

QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pixMap);
scene.addItem(pmItem); // only one item in the scene
//view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); // no difference
view.show();
Run Code Online (Sandbox Code Playgroud)

简化的基于QLabel的代码是:

void MyQLabel::mouseMoveEvent(QMouseEvent *event){
    if(_leftButtonPressed) {
            // calculate new {_x, _y} position
            repaint();
        }
    } else super::mouseMoveEvent(event);
}
void MyQLabel::paintEvent(QPaintEvent *aEvent){
    QPainter paint(this);
    paint.drawPixmap(_x, _y, pixMap);
}

... // Somewhere in the code:
MyQLabel _myLabel(NULL);
_myLabel.showFullScreen();
_myLabel.show();
Run Code Online (Sandbox Code Playgroud)

感觉QGraphicsView正在跳过一些位置(快速拖动),而QLabel在所有中间图像上绘制.

我错过了什么?

谢谢Alex

Ben*_*n T 1

update()当检测到滚动更改时,QGraphicsView 可能会调用。当你的标签呼唤repaint().

不同之处在于,update()安排一次呼叫repaint()和多次快速呼叫update()可能会呼叫repaint()一次。

快速拖动时,您可能会在短时间内注册多个鼠标事件。QGraphicsView 将处理所有这些,并为每个调用调用update(),并且只有在它们全部处理完毕后才repaint()真正调用。您的标签将为repaint()每个鼠标事件强制执行 a 。

您的标签可能比图形视图更平滑,但它会消耗更多资源,并且在有限的硬件上,标签将落后于鼠标光标,因为硬件正在尝试处理所有重绘。