Results 1 to 7 of 7

Thread: How to put custom handle image in QSlider using code?

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question 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:

    Qt Code:
    1. QSlider::handle:horizontal {
    2. image: url(images/track-marker.png);
    3. margin: -4px 0;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to put custom handle image in QSlider using code?

    You can use Qt's resource system.

  3. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to put custom handle image in QSlider using code?

    Please elaborate a bit. Are you talking about the .qrc file?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.
    Last edited by faldzip; 29th January 2009 at 00:12.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    montylee (29th January 2009)

  6. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to put custom handle image in QSlider using code?

    Quote Originally Posted by faldżip View Post
    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
    Qt Code:
    1. <file>images/track-marker.png</file>
    To copy to clipboard, switch view to plain text mode 
    to my project's qrc file and added:
    Qt Code:
    1. image: url(:/images/track-marker.png);
    To copy to clipboard, switch view to plain text mode 

    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.

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to put custom handle image in QSlider using code?

    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:

    Qt Code:
    1. <file alias="track-marker" >images/track-marker.png</file>
    To copy to clipboard, switch view to plain text mode 

    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):
    Qt Code:
    1. <qresource prefix="/images" >
    2. <file alias="tb_pressed" >Resources/toolbutton_pressed.png</file>
    3. <file alias="next-page" >Resources/16-em-down.png</file>
    4. <file alias="prev-page" >Resources/16-em-open.png</file>
    5. </qresource>
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by faldzip; 29th January 2009 at 19:56.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #7
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to put custom handle image in QSlider using code?

    Thanks for the info!
    Aliases are cool

Similar Threads

  1. QSlider custom handle image not displayed
    By planglois in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2008, 14:49
  2. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 22:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.