PDA

View Full Version : QPixmap and QLabel performance



mass85
8th December 2010, 23:29
I need to implement widget based on QToolButton that will allow display up to 4 icons on top of it. Icons will be moved, hidden, shown and changed. There will be many such widgets displayed at the same time and all of them will use same set of icons.
I am worried about performance in this case, because I expect operations of loading graphics, moving it and hiding to be slow. For some reasons function that will arrange icons according to some set of variables will be called many times. Values of these variables not necessarily will be changed between consecutive calls, but we don't want to keep a copy of set of values during previous call of this function.

I'm considering using 4 QLabels as containers for QPixmaps. I want to create static or global QPixmap variables, so I can create them once at start of application and use them to display icons simultanously on many such widgets. I have some questions related to this:

1. Is QLabel::setPixmap() optimised so it does not cause refreshment of window after loading same QPixmap? Or should I always compare previous QPixmap in this label with the new to set?
2. Are moving, hiding and displaying QLabels with QPixmaps slow operations? Should I check the position and visibility of QLabel to avoid situation, when I want to move QLabel to the current position or set visible already visible QLabel, to increase performance?

high_flyer
9th December 2010, 12:14
I am worried about performance in this case, because I expect operations of loading graphics, moving it and hiding to be slow.
this is not an issue if you use resources.
Have a look at Qt resource system.
http://doc.trolltech.com/4.7/resources.html
This way the image files are compiled in to the exe and you don't have to worry about loading from disk.

mass85
9th December 2010, 16:24
I know they are compiled in application.

I'm sorry I was not very precise. I meant "displaying" instead of "loading" (calling functions show() and also hide()).

high_flyer
9th December 2010, 16:46
know they are compiled in application.
So why are you doing this for:

I want to create static or global QPixmap variables, so I can create them once at start of application and use them to display icons simultanously on many such widgets.
It looks to me you are not really aware of what resources are and how they are integrated in to the application - be sure to read the link I posted previously.


I meant "displaying" instead of "loading" (calling functions show() and also hide()).
Unless you are showing and hiding the images in a very high rate, (lets say less than 30 ms ) this should not be a problem.
You are not showing video in your buttons are you?
Just switching between images is neglectable as far as performance is concerned, on any relatively modern hardware.

mass85
9th December 2010, 16:59
So why are you doing this for:

It looks to me you are not really aware of what resources are and how they are integrated in to the application - be sure to read the link I posted previously.

I must admit that I could not find detailed information about rendering images, creation of QPixmap objects etc. So I don't really understand how it works and don't know how to write optimised code.

My application runs on embedded Linux, where performance is important. Architucture of this application has some weak points, but I can't change it at the moment. It runs slow already without any icons. So I would like to know if performance decreases when I call label.setPixmap(QPixmap(":/icons/icon.png")) many times in a row.

By the way what format of picture would you advise in terms of performance? Png, gif, svg? Does it matter at all? I can just guess.

wysota
9th December 2010, 17:01
By the way what format of picture would you advise in terms of performance? Png, gif, svg? Does it matter at all? I can just guess.
Once you load it, it doesn't matter. Just don't reload the same file again and again.

mass85
12th December 2010, 14:41
I've changed my mind and now I don't want to compile all resources into application, but load them during runtime instead. According to my experiments I can't use static pointers to QPixmap as class members and create QPixmap objects just once in constructor of my class (load icons directly from disk), because application crashes. When these pointers are not static, application does not crash, but it's not a solution, becuase pixmaps are loaded every time we create another instance of this class.

So I think I will have to use external resource file. I'm not sure if I know how to do it, some questions arise:

1. After I create myresource.rcc file using

rcc -binary myresource.qrc -o myresource.rcc
and load them in main() function before creating any windows:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QResource::registerResource("/path/to/myresource.rcc");
MainWindow w;
w.show();

return a.exec();
}
all icons will be accessible in the same way as they were compiled in application? This would mean that this code will work:

myLabel.setPixmap(QPixmap(":/icons/icon.png"));

2. If I'm right about the above, I would like to automate the process of generating myresource.rcc. I guess that when I add myresource.qrc file to my project and build application, all resources are compiled in. I don't want them to be compiled in, but I want them to be accessible from my project in QT Creator. Is this possible?

3. Anyway would you advise using files *.rcc or registering resources directly from files *.png?

===
And some other important questions:
4. If I load 3 pixmaps to 3 different labels in a row, when screen is refreshed (function Paint() or however it is named, is called)? Is it refreshed after each pixmap setting or optimised and refreshed just once?

5. If I have 3 labels in some QLayout and I hide two of them, when will mechanism processing layouts start its work? After each single act of hiding or just once?

wysota
12th December 2010, 17:12
In general have a look at QPixmapCache.

mass85
12th December 2010, 22:53
QPixmapCache is an interesting solution, but could anyone answer to my questions? I answered to first question on my own by implementing and checking - it works as I thought. But rest of the questions are not so easy.

wysota
12th December 2010, 23:17
2. Are moving, hiding and displaying QLabels with QPixmaps slow operations? Should I check the position and visibility of QLabel to avoid situation, when I want to move QLabel to the current position or set visible already visible QLabel, to increase performance?

As in most situations the answer is "it depends on".


all icons will be accessible in the same way as they were compiled in application?
They will be accessible by paths you place them at in the resource file.


2. If I'm right about the above, I would like to automate the process of generating myresource.rcc. I guess that when I add myresource.qrc file to my project and build application, all resources are compiled in. I don't want them to be compiled in, but I want them to be accessible from my project in QT Creator. Is this possible?
Then they won't be resources anymore. What you want is access to files using a relative path. You don't need the resource system for that, just have a point of reference (like QCoreApplication::applicationDirPath()) and construct all paths based on that reference point.


3. Anyway would you advise using files *.rcc or registering resources directly from files *.png?
"it depends on".


4. If I load 3 pixmaps to 3 different labels in a row, when screen is refreshed (function Paint() or however it is named, is called)? Is it refreshed after each pixmap setting or optimised and refreshed just once?
It is refreshed when the control returns to the event loop and appropriate events scheduled by setting the pixmaps are processed (so if you assign 3 pixmaps one after the other, the widgets will be refreshed after all three pixmaps have already been set).


5. If I have 3 labels in some QLayout and I hide two of them, when will mechanism processing layouts start its work? After each single act of hiding or just once?
An event is scheduled for the layout to invalidate itself so when it is recalculated, both changes will be processed in one go.

mass85
13th December 2010, 11:19
Then they won't be resources anymore. What you want is access to files using a relative path. You don't need the resource system for that, just have a point of reference (like QCoreApplication::applicationDirPath()) and construct all paths based on that reference point.

I want to use resources, but load them from resources.rcc to my application when it starts, I don't want them to be build in application.

The problem is just the way I generate resources.rcc file. I want this file to be generated from resources.qrc during compilation of application. If it is possible I want resources.qrc file to be visible in QT Creator project.