Results 1 to 11 of 11

Thread: Customize QDial

  1. #1
    Join Date
    Mar 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Customize QDial

    Hi,

    I would like to change the look of QDial. More precisely: I would like to change the color of the position indicator or replace the bitmap that is used to draw it (if so).

    Subclassing is the way to go, no doubt, but where do I go from there? paintEvent does not get me anywhere.

    Any ideas on this?

    Thanks, JS

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Customize QDial

    Subclassing is the way to go
    I am not sure about that (all though it definitely is a way).
    Try using a style sheet with the ::groove or ::handle sub control.
    It just one line of code, so its easy and quick to test.
    Last edited by high_flyer; 8th December 2010 at 13:32.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customize QDial

    I can't change the default graphics is any way, but using paintEvent I can superimpose a little pixmap on the dial. And that is what I did. It just takes a little math to figure out how to translate the linear values of the dial into circular coordinates. Works like a charm.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Customize QDial

    I can't change the default graphics is any way, but using paintEvent I can superimpose a little pixmap on the dial.
    Well, I am not sure what you mean, since overloading paintEvent() IS changing the default painting.
    With style sheets you actually are not touching the original painting code, and don't need to subclass/override, and you have a grater level of customization with out the need to recompile the code if you load the style sheet from a file at runtime.
    But, if you have solution that works for you, that is fine!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customize QDial

    The truth be told: I could not figure out how to change the handle using styles. If I had a QDial named my_dial and an image loaded in a QPixmap named my_hanle ... how would I proceed?

    I searched the net and came up with something like

    QDial::handle:horizontal { height:6px; width:6px; image:my_handle; } ;

    but just adding that line does not work (and I'd be surprised if it did).

    Could you give me a complete example for this situation please?

    thanks,

    JS

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Customize QDial

    No worries, this forum is for asking questions,just be prepared to read and think if you really want to get to answers.
    So, as starters, here is the full Qt Sylesheets doc:
    http://doc.trolltech.com/4.7/stylesheet.html

    Could you give me a complete example for this situation please?
    pMyDial->setStyleSheet("#pMyDial::handle { color: red }");

    This is what I meant that its just one line of code, and should be easy to test if QDial indeed allows styling with stylesheets. (The documentation only specifies its parent classes)
    You can also use 'background' with an image, or a color.

    Note1: I didn't run this in a compiler, so if it doesn't compile, try to figure it out or ask again.
    Note2: This is only one way of doing this. If you read the docs you will see other ways as well.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Mar 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customize QDial

    Thanks!

    I tried various options (in the subclass, since it was there anyway). For example:

    this->setStyleSheet("#QDial::handle:horizontal { height:6px; width:6px; image:img_handle;}");

    I replaced QDial with 'this' and also tested your 'color' example.

    None had any effect. I was afraid of that because QDial does not even change when I choose a different desktop style, as opposed to QSlider and other widgets.

    Back to the other method...

    cheers,

    --
    JS

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Customize QDial

    this->setStyleSheet("#QDial::handle:horizontal { height:6px; width:6px; image:img_handle;}");
    I would call this from outside the object it self, since there you can specify the specific obejct as I have in my style sheet.
    You probably don't need horizontal, since it makes no sense in the QDial.
    I would not go for image for starters, since the chance you have problems with the path resolution is high (as you are totally new to this)
    The '#" selctor selcts object no classes, see my example code.

    However:
    I was afraid of that because QDial does not even change when I choose a different desktop style, as opposed to QSlider and other widgets.
    I share that concern, which is why I offered the one line test option.
    If may very well be the QDial has a spacial rendering which does not adjust to styles.
    In that case, you will really have to override paintEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Mar 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Customize QDial

    I now used QDial as provided (no subclassing).

    dial_volume = new QDial();

    next

    dial_volume->setStyleSheet("QDial::handle { color: red }");

    or

    dial_volume->setStyleSheet("#dial_volume::handle { color: red }");

    has no effect.

    That settles it I suppose. QDial has a mind of its own

    Thanks for your help.

    JS

  10. #10
    Join Date
    Feb 2010
    Posts
    99
    Thanks
    31
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Customize QDial

    I also had similar requirement to customize QDial. All I could do using stylesheet is change the background color. I ended up subclassing QDial and handling paintEvent().

  11. #11
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: Customize QDial

    Quote Originally Posted by Jayes View Post
    I can't change the default graphics is any way, but using paintEvent I can superimpose a little pixmap on the dial. And that is what I did. It just takes a little math to figure out how to translate the linear values of the dial into circular coordinates. Works like a charm.
    Could someone please elaborate on how to overload paintEvent() for QDial or show an example? How do I figure out which parts of the widget I need to draw, etc?

Similar Threads

  1. QDial in Qt4.6 is without needle?
    By raedbenz in forum Qt Programming
    Replies: 9
    Last Post: 16th July 2010, 14:57
  2. Trying to create custom QDial
    By dpatel in forum Newbie
    Replies: 3
    Last Post: 16th March 2010, 12:33
  3. QDial looks too ugly, Why not improve it
    By lmax in forum Qt Tools
    Replies: 2
    Last Post: 13th March 2009, 21:03
  4. How can I resize needle of QDial
    By validator in forum Qt Tools
    Replies: 1
    Last Post: 1st February 2008, 10:10
  5. QDial
    By mickey in forum Newbie
    Replies: 4
    Last Post: 18th June 2006, 12:01

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.