I would like the QDial of the rotary encoder not to move when clicked with the mouse but only when it is dragged.
There is a way to get this into a QDial.
Greetings
There is a way to get this into a QDial.
Greetings
bool KY040::eventFilter(QObject *object, QEvent *event)
{
bool ret = false;
if ( object == m_dial && ( event->type() == QEvent::MouseButtonPress ) ) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
if(mouseEvent->buttons() == Qt::LeftButton) {
//QStyleOptionSlider *opt = new QStyleOptionSlider;
QStyleOptionComplex *opt = new QStyleOptionComplex;
opt->initFrom(m_dial);
QRect sr = m_dial->style()->subControlRect(QStyle::CC_Dial, opt, QStyle::SC_DialHandle, m_dial);
qDebug() << sr;
if (!sr.contains(mouseEvent->pos())){
event->ignore();
ret = true;
qDebug() << "LEFT";
}
}
}
return ret;
}
QStyleOptionSlider *opt = new QStyleOptionSlider;
opt->initFrom(scrollBar1);
QRect r = scrollBar1->style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarSubLine);
bool KY040::eventFilter( QObject *object, QEvent *event )
{
bool ret = true;
if( object == m_dial && event->type() == QEvent::MouseButtonPress )
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
if(mouseEvent->buttons() == Qt::LeftButton) ret = false;
}
return ret;
}
bool KY040::eventFilter(QObject *object, QEvent *event)
{
bool ret = false;
int sliderpos = m_dial->sliderPosition();
if (sliderpos == maximum) sliderpos = 0;
if ( object == m_dial && ( event->type() == QEvent::MouseButtonPress ) ) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*> (event);
int frompoint = valueFromPoint(mouseEvent->pos());
if (frompoint == maximum) frompoint = 0;
if(mouseEvent->buttons() == Qt::LeftButton) {
// qDebug() << valueFromPoint(mouseEvent->pos()) << m_dial->sliderPosition();
if (sliderpos != frompoint) {
event->ignore();
ret = true;
}
else event->accept();
}
}
return ret;
}
int KY040::valueFromPoint(const QPoint &p)
{
double yy = m_dial->height()/2.0 - p.y();
double xx = p.x() - m_dial->width()/2.0;
double a = (xx || yy) ? std::atan2(yy, xx) : 0;
if (a < (Q_PI / -2))
a = a + Q_PI * 2;
int dist = 0;
int minv = minimum, maxv = maximum;
if (minimum < 0) {
dist = -minimum;
minv = 0;
maxv = maximum + dist;
}
int r = maxv - minv;
int v;
if (wrapping)
v = (int)(0.5 + minv + r * (Q_PI * 3 / 2 - a) / (2 * Q_PI));
else
v = (int)(0.5 + minv + r* (Q_PI * 4 / 3 - a) / (Q_PI * 10 / 6));
if (dist > 0)
v -= dist;
return !invertedAppearance ? bound(v) : maximum - bound(v);
}
int KY040::bound(int val)
{
if (wrapping) {
if ((val >= minimum) && (val <= maximum))
return val;
val = minimum + ((val - minimum) % (maximum - minimum));
if (val < minimum)
val += maximum - minimum;
return val;
} else {
return qMax(minimum, qMin(maximum, val));
}
}
Nice, I will try it.I was unable to reimplement QDial but I did this:
Similar topics
Permissions in this forum:
You cannot reply to topics in this forum
|
|