Results 1 to 14 of 14

Thread: Problems customizing QSlider

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

    Default Problems customizing QSlider

    This is a copy of a message posted to the qt-interest list. The list was of no help.

    I've been working on a class to implement a QSlider with a custom pixmap
    instead of the default handle graphics. Inheriting QSilder and overriding
    the paintEvent method seemed too clunky, so instead I opted to create a new
    style and override the drawComplexControl method. That basically works, with
    two problems:
    1) The pixmap is clipped by the QSlider boundary, despite me overriding the
    contentsRect() method of QSLider with my own pixmap-aware implementation.
    2) For some reason, dragging the slider only works when I use some other
    combination of mouse buttons that's not just LMB. So if I press right+left,
    I can drag just find, but just left mouse button alone doesn't do anything.
    This is odd because I haven't touched mouseEvents.

    Any help will be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems customizing QSlider

    Maybe it's because QSlider doesn't know the exact size of your pixmap?

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

    Default Re: Problems customizing QSlider

    That's what I'm guessing, but what do I need to override to tell it that? None of the pixelMetrics seem relevant.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems customizing QSlider

    How about QStyle::pixelMetric(QStyle::PM_SliderThickness)? Seems to be used in QSlider::sizeHint(). You can dig a lot of valuable information by taking a look at QSlider sources..
    J-P Nurmi

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

    Antrax (8th October 2006)

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

    Default Re: Problems customizing QSlider

    Nice catch, thanks. I assumed that metric referred to the width of the groove the slider moved in. Apparently, it doesn't

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

    Default Re: Problems customizing QSlider

    That worked like a charm. Now, however, there's still some horizontal clipping. I've tried reimplementing QStyle::PM_SliderLength and QStyle::PM_SliderSpaceAvailable and neither helped. What metric am I overlooking this time?

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems customizing QSlider

    This is just a guess, but maybe QStyle::sizeFromContents(QStyle::CT_Slider)? I see it being used in QSlider::sizeHint().
    J-P Nurmi

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

    Default Re: Problems customizing QSlider

    Nope, no luck this time

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems customizing QSlider

    About the "horizontal clipping".. what's the orientation of the slider? Mind giving out a screenshot of what you've achieved so far?
    And what about QStyle::subControlRect(QStyle::SC_SliderHandle), have you taken care of that? Is your style using this area for painting the handle pixmap?
    J-P Nurmi

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

    Default Re: Problems customizing QSlider

    from drawComplexControl:
    Qt Code:
    1. QStyleOptionSlider NewOptions(*StyleOptions);
    2. NewOptions.subControls &= ~SC_SliderHandle;
    3. DefaultStyle->drawComplexControl(control, &NewOptions, painter, widget);
    4.  
    5. //manually drawing the handle
    6. QRect HandlePos = DefaultStyle->subControlRect(CC_Slider, option, SC_SliderHandle);
    7. QRect PicRect = sliderPicture->rect();
    8.  
    9. PicRect.moveCenter(HandlePos.center());
    10. painter->drawPixmap(PicRect, *sliderPicture, sliderPicture->rect());
    To copy to clipboard, switch view to plain text mode 

    from pixelMetric:
    Qt Code:
    1. QStyle* DefaultStyle = QApplication::style();
    2.  
    3. if (QStyle::PM_SliderSpaceAvailable == metric)
    4. return DefaultStyle->pixelMetric(metric, option, widget) - sliderPicture->width();
    5.  
    6. if (QStyle::PM_SliderThickness == metric)
    7. return sliderPicture->height();
    8.  
    9. return DefaultStyle->pixelMetric(metric, option, widget);
    To copy to clipboard, switch view to plain text mode 

    The slider is horizontal, so when the handle is at one of the extreme edged, the pixmap is clipped. I'll post a picture later today. Thanks!

  12. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems customizing QSlider

    This is from src/gui/widget/qslider.cpp:

    Qt Code:
    1. /*!
    2.   \reimp
    3. */
    4. QSize QSlider::sizeHint() const
    5. {
    6. Q_D(const QSlider);
    7. ensurePolished();
    8. const int SliderLength = 84, TickSpace = 5;
    9. QStyleOptionSlider opt = d->getStyleOption();
    10. int thick = style()->pixelMetric(QStyle::PM_SliderThickness, &opt, this);
    11. if (d->tickPosition & TicksAbove)
    12. thick += TickSpace;
    13. if (d->tickPosition & TicksBelow)
    14. thick += TickSpace;
    15. int w = thick, h = SliderLength;
    16. if (d->orientation == Qt::Horizontal) {
    17. w = SliderLength;
    18. h = thick;
    19. }
    20. return style()->sizeFromContents(QStyle::CT_Slider, &opt, QSize(w, h), this).expandedTo(QApplication::globalStrut());
    21. }
    22.  
    23. /*!
    24.   \reimp
    25. */
    26. QSize QSlider::minimumSizeHint() const
    27. {
    28. Q_D(const QSlider);
    29. QSize s = sizeHint();
    30. QStyleOptionSlider opt = d->getStyleOption();
    31. int length = style()->pixelMetric(QStyle::PM_SliderLength, &opt, this);
    32. if (d->orientation == Qt::Horizontal)
    33. s.setWidth(length);
    34. else
    35. s.setHeight(length);
    36. return s;
    37. }
    To copy to clipboard, switch view to plain text mode 

    This is the size a QSlider requests when being in a layout. If you already have taken care of QStyle::pixelMetric(QStyle::PM_SliderLength), the only choice left is QStyle::sizeFromContents(QStyle::CT_Slider). You should somehow make sure there is enough padding space at the ends, not just test if the pixmap would fit into the given size.
    Last edited by jpn; 11th October 2006 at 18:47. Reason: Disabled stupid smilies
    J-P Nurmi

  13. The following user says thank you to jpn for this useful post:

    Antrax (11th October 2006)

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

    Default Re: Problems customizing QSlider

    I have to say I'm completely stumped. I've read some style files and qslider and qabstractslider, and I've tried overriding every single thing they use, and all to no avail.
    So I went for a cop-out and implemented a PictureSlider class, overrided the setMaximum and setMinimum to add a little extra, and manually kept the slider just a little before the groove edge by catching valueChanged. It's not the way this thing's supposed to work, but at least it works

  15. #13
    Join Date
    Aug 2011
    Location
    Bangalore, India
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows Symbian S60

    Default Re: Problems customizing QSlider

    Antrax can you please post your whole code. I am also trying to make custom slider using pixmap but i am not getting the proper output.

  16. #14
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems customizing QSlider

    I have same problem I want to change vertical scrollbar thickness but I didn't change it....

    Can You help me out for the same???


    kinjal.

Similar Threads

  1. QSlider Update Problem
    By December in forum Qt Programming
    Replies: 4
    Last Post: 10th September 2006, 05:02
  2. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 16:39
  3. Video and QSlider
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 1st May 2006, 20: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.