2

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

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,

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,

Akhil V Suku
  • 870
  • 2
  • 13
  • 34
  • 2
    Have you bothered to profile them and find out? – cmannett85 Feb 26 '15 at 09:10
  • right now I am using the fist method. Because I was not much familiar with QPainter. I have read that drawing using paintEvent will reduce the speed, since it will execute whenever a screen update request comes. – Akhil V Suku Feb 26 '15 at 09:15

2 Answers2

4

From a theoretical perspective, the QPainter approach will be faster because the overhead introduced by QLabel is avoided. Internally QLabel needs to use a QPainter as well (using 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.

Matthias Kuhn
  • 1,162
  • 2
  • 14
  • 42
  • 1
    "From a theoretical perspective" - and practically too! – TheDarkKnight Feb 26 '15 at 09:25
  • Practically QLabel does some caching and uses a different method than the one provided in the example (drawPixmap vs drawPicture). In practice it will depend on the QPainter implementation and usage (scaling involved etc.) if it is faster. I am sure that there is the possibility to implement a QPainter based approach that is slower than QLabel. – Matthias Kuhn Feb 26 '15 at 09:35
1

If you have to draw more than 100 of this, this usually means that you should not use any of those solutions.
Most probably QListView with custom delegate and QAbstractListModel to hold those images is what you really need (or table version).

Marek R
  • 32,568
  • 6
  • 55
  • 140