PDA

View Full Version : How to put custom handle image in QSlider using code?



montylee
28th January 2009, 20:47
I want to put a custom image as the handle displayed in QSlider. I am using the following style sheet code:


QSlider::handle:horizontal {
image: url(images/track-marker.png);
margin: -4px 0;
}

The above code works but the problem is that the directory where the handle image is located is not fixed. So, in the source code, i can get the directory location but i don't know how to pass that location to the style sheet.

Is there any other way that through source code, i can set the handle image? One more thing, i am using style sheet to fill the groove color and other variables so style sheet will still be used along with the code.

wysota
28th January 2009, 23:00
You can use Qt's resource system.

montylee
28th January 2009, 23:37
Please elaborate a bit. Are you talking about the .qrc file?

faldzip
29th January 2009, 00:07
With Qt's resource system (.qrc file) you can compile resources into your executable file so you're sure that your resource is always in the same location starting with ":/". That path is also visible in style sheets. Whats more, you can even put your style sheet on .qrc. Look at http://doc.trolltech.com/4.4/resources.html for more info.

montylee
29th January 2009, 18:17
With Qt's resource system (.qrc file) you can compile resources into your executable file so you're sure that your resource is always in the same location starting with ":/". That path is also visible in style sheets. Whats more, you can even put your style sheet on .qrc. Look at http://doc.trolltech.com/4.4/resources.html for more info.
ok, i added something like

<file>images/track-marker.png</file>
to my project's qrc file and added:

image: url(:/images/track-marker.png);

to the style sheet file. I am able to get the icon displayed on a different machine so it means that the icon has been compiled into the executable but i have one query:

Why do i need to give the path as ":/images/track-marker.png" in the style sheet file. I am able to see the icon in a different machine where there is no "images" folder. If i change the path in style sheet to "track-marker.png" or something else instead of ":/images/track-marker.png", the image is not displayed.

So, my problem is solved but that doubt remains.

faldzip
29th January 2009, 19:47
Why do i need to give the path as ":/images/track-marker.png" in the style sheet file. I am able to see the icon in a different machine where there is no "images" folder. If i change the path in style sheet to "track-marker.png" or something else instead of ":/images/track-marker.png", the image is not displayed.

because in .qrc file you have to put path to a file to let RCC know what file you want to put into exec. So the path you use in code is the path to file (so the path which you would use normally) but with a ":/" prefix (as it's qrc prefix). But you can do something like this:



<file alias="track-marker" >images/track-marker.png</file>


and now you can use path ":/track-marker"
But the common way (in my opinion) is to do it (in my app it looks something like this):


<qresource prefix="/images" >
<file alias="tb_pressed" >Resources/toolbutton_pressed.png</file>
<file alias="next-page" >Resources/16-em-down.png</file>
<file alias="prev-page" >Resources/16-em-open.png</file>
</qresource>

so I have aliases to replace complicated paths, but I add prefix to group resources. Paths look like ":/images/next-page" as it is prefix + alias.

montylee
29th January 2009, 20:38
Thanks for the info!
Aliases are cool :)