Results 1 to 5 of 5

Thread: QSlider mid position

  1. #1
    Join Date
    Jan 2020
    Posts
    47
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QSlider mid position

    slider.png
    I want a slider with the mid position as default. I can set the slider to the mid position, but I cannot find a way to set the grove colour to grey when the slider is in the mid position. What I am trying to achieve is for the blue colour to stretch out left/right from the mid position, instead of from the leftmost position. I presume that this is a common enough issue for a solution to excist?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSlider mid position

    I presume that this is a common enough issue for a solution to exist?
    Well, actually, no. The out-of-the-box slider only fills half of the trough, either from the minimum up to the current slider position, or from the maximum down (if invertedAppearance() is set). There doesn't appear to be any way of filling both sides of the trough using the standard widget.

    Drawing of complex widgets is deeply embedded in the Qt implementation and depends on the platform. You could derive your own slider from QSlider and reimplement the paintEvent() method, but I invite you to look at the Qt source code for QSlider::paintEvent(), and follow that into the call to QStyle::drawComplexControl(). The specific QStyle derived class which is actually used for drawing depends on the platform. So for example, in QWindowsStyle::drawComplexControl() there are over 215 lines of code just to draw a slider.

    You might be better off trying to find something online (like at linux-apps.com or qinclude.org) or by studying the QStyle documentation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSlider mid position

    You could apply a default stylesheet and monitor the range change and value change slots and apply a different stylesheet when the mid-value is reached. The important parts of the style sheet might look like this:
    Qt Code:
    1. QSlider::add-page:vertical { background: blue; }
    2. QSlider::sub-page:vertical { background: blue; }
    To copy to clipboard, switch view to plain text mode 
    when off-centre, and this when centred (probably with some tolerance around centre):
    Qt Code:
    1. QSlider::add-page:vertical { background: grey; }
    2. QSlider::sub-page:vertical { background: grey; }
    To copy to clipboard, switch view to plain text mode 
    You would probably want to style the rest of the slider; see https://doc.qt.io/qt-5/stylesheet-ex...ing-qscrollbar
    You need to reapply the entire stylesheet each time.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSlider mid position

    Maybe I misunderstood the OP's question. I assumed he wanted the background color to be the same on both sides no matter where the slider was positioned.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2020
    Posts
    47
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QSlider mid position

    slider.png
    The stylesheet idea worked: I got rid of the blue grove by using the following stylesheet;
    QSlider::groove:horizontal {
    border: 1px solid #999999;
    height: 20px;
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
    margin: 2px 0;
    }

    QSlider::handle:horizontal {
    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
    border: 1px solid #5c5c5c;
    width: 30px;
    margin: -2px 0px;
    }
    Thanks for the advise.

Similar Threads

  1. How to store QSlider position Value into .txt file?
    By YDYD in forum Qt Programming
    Replies: 26
    Last Post: 3rd July 2014, 11:17
  2. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 05:46
  3. Replies: 4
    Last Post: 1st December 2011, 20:53
  4. QSlider : How to hint current position value ?
    By andre_teprom in forum Newbie
    Replies: 2
    Last Post: 10th August 2011, 02:19
  5. initial position of a QSlider
    By franco.amato in forum Qt Programming
    Replies: 9
    Last Post: 24th September 2008, 19:37

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.