Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclass QDial
Forum Updated to NodeBB v4.3 + New Features

Subclass QDial

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PuntP Offline
    PuntP Offline
    Punt
    wrote on last edited by Punt
    #1

    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
    {
        Q_OBJECT
    public:
        CustomDial(QWidget * parent = nullptr);
    
    private:
    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)
    {
        Q_D(QDial);
        if (d->maximum == d->minimum ||
            (e->button() != Qt::LeftButton)  ||
            (e->buttons() ^ e->button())) {
            e->ignore();
            return;
        }
        e->accept();
        setSliderPosition(d->valueFromPoint(e->pos()));
        // ### 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.
        setSliderDown(true);
    }
    

    and tried to make some adjustment :

    void CustomDial::mousePressEvent(QMouseEvent* e)
    {
        int oldValue = value();
    
        //copy the beggining of Qt source :
    Q_D(QDial);
        if (d->maximum == d->minimum ||
            (e->button() != Qt::LeftButton)  ||
            (e->buttons() ^ e->button())) {
            e->ignore();
            return;
        }
        e->accept();
        //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);
       else
          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...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      you are trying to call private stuff.
      http://zchydem.enume.net/2010/01/19/qt-howto-private-classes-and-d-pointers/

      https://forum.qt.io/topic/516/why-the-excessive-use-of-private-classes/2

      PuntP 1 Reply Last reply
      1
      • mrjjM mrjj

        hi
        you are trying to call private stuff.
        http://zchydem.enume.net/2010/01/19/qt-howto-private-classes-and-d-pointers/

        https://forum.qt.io/topic/516/why-the-excessive-use-of-private-classes/2

        PuntP Offline
        PuntP Offline
        Punt
        wrote on last edited by
        #3

        @mrjj

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

        mrjjM 1 Reply Last reply
        0
        • PuntP Punt

          @mrjj

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Punt said:

          QDial

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            michelson
            wrote on last edited by
            #5

            How about:

            1. In slot connected to sliderPressed save old value
            2. Set tracking http://doc.qt.io/qt-4.8/qabstractslider.html#tracking-prop
            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
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved