Subclass QDial

  • Hi, I would like to subclass QDial because :

    Here the QDial can jump values (ex : what I want : If QDial.value() = 2, I want the QDial able to move only to 1 or 3.

    So I tried :

    #ifndef CUSTOMDIAL_H
    #define CUSTOMDIAL_H
    #include <QObject>
    #include <QDial>
    #include <QMouseEvent>
    class CustomDial : public QDial
        CustomDial(QWidget * parent = nullptr);
    virtual void mousePressEvent(QMouseEvent *e) override;
    #endif // CUSTOMDIAL_H

    I started to try only by overriding mousePressEvent() and if this work I'll also do it for other mouse-related function.

    In order to do that, I took mousePressEvent from Qt source:

    void QDial::mousePressEvent(QMouseEvent *e)
        if (d->maximum == d->minimum ||
            (e->button() != Qt::LeftButton)  ||
            (e->buttons() ^ e->button())) {
        // ### This isn't quite right,
        // we should be doing a hit test and only setting this if it's
        // the actual dial thingie (similar to what QSlider does), but we have no
        // subControls for QDial.

    and tried to make some adjustment :

    void CustomDial::mousePressEvent(QMouseEvent* e)
        int oldValue = value();
        //copy the beggining of Qt source :
        if (d->maximum == d->minimum ||
            (e->button() != Qt::LeftButton)  ||
            (e->buttons() ^ e->button())) {
        //And now change the setValue if there is a difference like this :
       int newValueFromMouse =  d->valueFromPoint(e->pos());
       int diff = newValueFromMouse - oldValue;
       if(diff > 1)
          setSliderPosition(oldValue +1);
       else if(diff < -1)
          setSliderPosition(oldValue -1);
          setSliderPosition( newValueFromMouse);

    In my opinion, the algorith should works right?

    But there is a problem with the variable 'd' :

    C:\Qt\5.6\mingw49_32\include\QtCore\qglobal.h:1018: erreur : 'QDialPrivate* QDial::d_func()' is private
         inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(qGetPtrHelper(d_ptr)); } \

    And I don't understand why ?! I mean I understand the problem, but I don't understand why there is this mistake if I just used Qt source code...

  • Qt Champions 2016

  • @mrjj

    Hi, thanks for your answer but..
    How do I procede ?

  • Qt Champions 2016

    @Punt said:


    well i never tried to use privates as they are private for a reason
    but i think you need to include
    private\xxx_p.h file but I dont see any for QDial.

    Sorry. Im not sure how to allow this.

  • How about:

    1. In slot connected to sliderPressed save old value
    2. Set tracking
    3. In slot connected to valueChanged signal have something like:
    //valueChanged(int value)
    if( value > (old_value+1)  )
       dial->setValue(old_value + 1);
    else if( value < (old_value-1) )
      dial->setValue(old_value - 1);

    Two regards:

    • not sure if i understood what you want to achive corectly
    • above code is not tested, just a sketch

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.