简体   繁体   English

QPainter或QLabel绘制QPixmap的成本较低

[英]QPainter or QLabel is less costly to draw QPixmap

I have to create an Icon pixmap, two methods I am familiarized to do that, One is setting the pixmap as o QLabel and display it, and other is drawing pixmap using QPainter , ie 我必须创建一个Icon像素图,我熟悉两种方法来做到这一点,一种是将像素图设置为QLabel并显示它,另一种是使用QPainter绘制像素图,即

Method one 方法一

Icon::Icon
{
    QLabel iconLab = new QLabel;
    QLabel iconName = new QLabel;
    iconLab->setPixmap("mypixmap.png"); 
    iconName->setText("myiconname");
    QVBoxLayout *iconLayout = new QVBoxLayout; 
    iconLayout->setMargin(0);
    iconLayout->addWidget(iconLab, 1, Qt::AlignCenter);
    iconLayout->addWidget(iconName, 1, Qt::AlignCenter);
    iconLayout->setSpacing(0);

    setLayout(iconLayout);
    setMaximumSize(100,160);
    setMinimumSize(100,160);
}

Method 2, 方法2

Icon::Icon
{     
    setMaximumSize(100,160);
    setMinimumSize(100,160);
}
Icon::paintEvent(QPaintEvent*)
{      
    QPainter painter;
    painter.drawPixmap(0,0,myPixmap);
    painter.drawText(0,100,myText)
}

I have to draw number of Icons, more than 100, so which one is effective, Thanks in advance, 我必须绘制多个图标,数量超过100个,所以哪个是有效的,在此先感谢,

From a theoretical perspective, the QPainter approach will be faster because the overhead introduced by QLabel is avoided. 从理论上讲,由于避免了QLabel引入的开销,因此QPainter方法将更快。 Internally QLabel needs to use a QPainter as well (using drawPicture() ). 在内部QLabel还需要使用QPainter(使用drawPicture() )。

However, it is questionable if this difference will make your application more responsive. 但是,是否存在这种差异是否会使您的应用程序具有更高的响应能力值得怀疑。 I doubt that this optimization will even be noticeable. 我怀疑这种优化是否会引人注目。

I would recommend to take care of code readability in the first place and take what is easier / feels better to use. 我建议首先注意代码的可读性,并选择更容易使用的感觉。

Once you have the functionality in place and there is a performance problem, you can start profiling and decide where the time and effort to optimize is best invested. 一旦具备了适当的功能并且出现了性能问题,就可以开始进行性能分析,并确定最佳的投入时间和精力。

If you have to draw more than 100 of this, this usually means that you should not use any of those solutions. 如果必须绘制100个以上的图形,这通常意味着您不应使用任何这些解决方案。
Most probably QListView with custom delegate and QAbstractListModel to hold those images is what you really need (or table version). QListView具有自定义委托和QAbstractListModel来保存这些图像的QListView可能是您真正需要的(或表版本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM