Unsolved 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 { 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...
-
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
-
Hi, thanks for your answer but..
How do I procede ? -
@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.
-
How about:
- In slot connected to
sliderPressed
save old value - Set tracking http://doc.qt.io/qt-4.8/qabstractslider.html#tracking-prop
- 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
- In slot connected to