Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Customizing a QSlider in a style subclass
Qt 6.11 is out! See what's new in the release blog

Customizing a QSlider in a style subclass

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xtraction
    wrote on last edited by
    #1

    Hello,

    I am making a custom style subclassing from one of Qt's style classes (QMacStyle) and I am trying to first of all, make the sliders that I use different. For this I want to change the slider handle to be an image I have to give it a different look.

    What I have tried so far is doing something like this:

    @
    void CustomStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter p, const QWidget w) const
    {
    if(cc == CC_Slider)
    {
    const QSlider
    slider = static_cast< const QSlider
    >(w);
    if(slider->orientation() == Qt::Horizontal){
    drawHSlider(SC_SliderHandle, opt, p);
    }
    }

    else
        return QMacStyle::drawComplexControl(cc, opt, p, w);
    

    }

    ...

    void CustomStyle::drawHSlider(SubControl handle, const QStyleOptionComplex *option, QPainter *painter) const
    {
    if(handle == SC_SliderHandle){
    QPixmap fader(":/images/hfader");
    QRect handleRect = subControlRect(CC_Slider, option, handle);

        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);
        painter->drawPixmap(handleRect, fader);
        painter->restore();
    }
    

    }
    @

    But I'm kinda lost on how to draw the image to the handle of the slider and be able to move the slider like usual. Any help I would really appreciate. Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Wouldn't it be easier to use a style sheet instead? Perhaps something like:

      @
      QSlider::handle {image:url(:/images/hfacer) }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved