Results 1 to 2 of 2

Thread: Override setValue() of QScrollBar

  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Override setValue() of QScrollBar

    Qt 4.8.x on Linux

    I'm in a situation of needing to override the setValue() method of a QScrollBar. The scrollbar is on a QTreeWidget. Yes, FlickCharm and several other things could be setting the position so, no, anything involving the mouse is out of the question. I think the real problem has to do with the Q_PROPERTY macro and the fact QAbstractSlider doesn't declare the slot virtual.

    Why override the setValue() slot? Simple. Many points can change that value. The valueChanged() signal is only emitted when setValue() is called with a DIFFERENT value than currently stored. I need value changed to emit no matter what. Why? Because if you are bumping into one end or the other of the tree we want to provide feedback to the user.

    Perhaps I just need more caffeine, but having trouble.

    First attempt was the least amount of code I could think of.

    Class declaration

    class ValueScrollBar : public QScrollBar
    {
    Q_OBJECT
    public:
    explicit ValueScrollBar(QWidget *parent = 0);
    explicit ValueScrollBar( Qt::Orientation orientation, QWidget *parent = 0);

    public slots:
    void setValue(int _value);
    };

    slot source
    void ValueScrollBar::setValue(int _value)
    {
    if (_value == value())
    emit valueChanged( _value);
    else
    QScrollBar::setValue( _value);
    }



    I can create one of these. Assign it as the vertical scrollbar for the tree widget. Add data to the tree, then deliberately call tree->verticalScrollBar()->setValue( 5) and never hit a break point in the slot source.

    Has anybody ever overridden the setValue() method for a scrollbar? Please tell me how you did it. I truly hope I don't have to roll my own scrollbar because of something short-sighted in QAbstractSlider.

    Thanks


    Added after 29 minutes:


    I just confirmed it. Very short sighted design on part of QAbstractSlider. None of the slots are virtual. When something uses, say, QTreeWidget->verticalScrollBar()->setValue() it is _always_ going to take the base implementation.
    (*%^*&^%(*&)(*&U)(*&(*&^*&^%*&^%
    Last edited by RolandHughes; 11th October 2014 at 17:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Override setValue() of QScrollBar

    Property change signals, as their name suggests, are only emitted on change of the property.
    This is to ensure that connections do not end up creating endless loops.

    The setters of most properties are therefore not virtual to ensure that a subclass does not accidentally breach that behavioral contract.

    You can of course violate that on any custom property, though it is obviously not very wise.

    Cheers,
    _

Similar Threads

  1. Override QScrollbar using QStyle
    By Alundra in forum Qt Programming
    Replies: 5
    Last Post: 21st June 2014, 12:11
  2. How to use setValue in QSqlTableModel?
    By jtdavidson in forum Newbie
    Replies: 1
    Last Post: 21st July 2010, 16:10
  3. SetValue
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 3rd October 2007, 23:45
  4. QProgressDialog::setValue crash( Mac )
    By marcel in forum Qt Programming
    Replies: 7
    Last Post: 12th April 2007, 00:12
  5. override wheelEvent for QScrollBar
    By ChasW in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 10:50

Tags for this Thread

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.