Results 1 to 2 of 2

Thread: Custom (pixmap) slider handle for QScrollBar

  1. #1
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom (pixmap) slider handle for QScrollBar

    Having accomplished something similar for a QSlider, I thought it'd be simple. However, I've overriden drawComplexControl, subControlRect, hitTestComplexControl and pixelMetric, and I still encouter two related bugs: when dragging the handle, the handle moves to other places than what the mouse cursor points to, and the handle, instead of being confined to the slider groove, manages to escape the QScrollBar and overwrite its arrows.
    I'm guessing both of these is because somehow I've screwed up the calculation of pixel position to slider position to value to screen position, but I can't figure out where. Below are cleaned up code snippets.

    Qt Code:
    1. void PictureScrollbarStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex* option,
    2. QPainter* painter, const QWidget* widget) const
    3. {
    4. -snip-
    5. //drawing the handle
    6. QRect Pos = subControlRect(CC_ScrollBar, option, SC_ScrollBarSlider, widget);
    7. painter->drawPixmap(Pos , *handlePicture);
    8. -snip-
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QRect PictureScrollbarStyle::subControlRect(ComplexControl control, const QStyleOptionComplex* option,
    2. SubControl subControl, const QWidget* widget) const
    3. {
    4. -snip-
    5. if (SC_ScrollBarSlider == subControl)
    6. {
    7. int h = handlePicture->height() / 2;
    8. int span = StyleOptions->maximum - StyleOptions->minimum;
    9. float ratio = (float)StyleOptions->rect.height() / (float)span;
    10. int position = (int)(StyleOptions->sliderPosition * ratio);
    11. QRect result(0, position - h, handlePicture->width(), handlePicture->height());
    12. return result.isValid() ? result : QRect(0, 0, 0, 0);
    13. }
    14. -snip-
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QStyle::SubControl PictureScrollbarStyle::hitTestComplexControl(ComplexControl control, const QStyleOptionComplex* option,
    2. const QPoint& pos, const QWidget* widget) const
    3. {
    4. -snip-
    5. r = subControlRect(CC_ScrollBar, option, SC_ScrollBarSlider, widget);
    6. if (r.isValid() && r.contains(pos))
    7. {
    8. return SC_ScrollBarSlider;
    9. }
    10.  
    11. return SC_ScrollBarGroove;
    12. }
    To copy to clipboard, switch view to plain text mode 

    pixelMetric code isn't attached because debug prints show it's not used.
    Thanks in advance.
    Last edited by jacek; 16th October 2006 at 20:36. Reason: wrapped too long lines

  2. #2
    Join Date
    Oct 2006
    Posts
    18
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom (pixmap) slider handle for QScrollBar

    Glad to see I'm not the only one stumped by this. I eventually worked around the problem by calling QApplication::style()->subControlRect for the handle subrect, made slight adjustments to top and bottom if needed to accomodate my slightly larger arrow graphics, and used a pixmap that looks good when scaled for handle graphics. The whole value/position thing is pretty tangled instead Qt's code (namely qstyle's positionFromValue and qlistwidget's code), so the setRange fix I used for QSlider couldn't work.

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.