How to put custom handle image in QSlider using code?
I want to put a custom image as the handle displayed in QSlider. I am using the following style sheet code:
Code:
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.
Re: How to put custom handle image in QSlider using code?
You can use Qt's resource system.
Re: How to put custom handle image in QSlider using code?
Please elaborate a bit. Are you talking about the .qrc file?
Re: How to put custom handle image in QSlider using code?
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.
Re: How to put custom handle image in QSlider using code?
Quote:
Originally Posted by
faldżip
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
Code:
<file>images/track-marker.png</file>
to my project's qrc file and added:
Code:
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.
Re: How to put custom handle image in QSlider using code?
Quote:
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:
Code:
<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):
Code:
<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.
Re: How to put custom handle image in QSlider using code?
Thanks for the info!
Aliases are cool :)