[英]How can I make my QDial react to a different mouse event than the one predefined?
I have a subclass of a QDial
and I want to change the values by moving my mouse up and down. 我有一个
QDial
的子类,我想通过上下移动鼠标来更改值。 At the moment, the basic implementation allows you to do a circular movement with the mouse to change the values. 目前,基本实现允许您使用鼠标进行圆周运动来更改值。 But I'd like to change them by moving my mouse either up (let's say that once I have pressed my dial, I'll move my mouse to the top of my screen) or down (to the bottom of the screen).
但是我想通过向上移动鼠标来改变它们(让我们说一旦我按下我的表盘,我将鼠标移动到我的屏幕顶部) 或向下移动 (到屏幕的底部)。
Here's a picture (beware, MS Paint skills ahead) of what I want to achieve. 这是我想要实现的图片(要注意,MS Paint技能未来)。 On the left, it's the basic behavior, and on the right, the behavior that I'd like to have.
在左边,这是基本行为,在右边,是我想要的行为。
I don't see how I could get that with mousePressEvent
or mouseMoveEvent
. 我不知道如何使用
mousePressEvent
或mouseMoveEvent
获得它。 Does anybody have an idea? 有人有想法吗?
Unless I've misunderstood what you want this should be fairly straight forward with something like... 除非我误解了你想要的东西,否则这应该是相当直接的......
class dial: public QDial {
public:
dial (QWidget *parent = nullptr)
: QDial(parent)
, m_dragging(false)
, m_scale_factor(1.0f)
{}
protected:
virtual void mousePressEvent (QMouseEvent *event) override
{
m_mouse_press_point = event->pos();
m_dragging = true;
m_base_value = value();
}
virtual void mouseReleaseEvent (QMouseEvent *event) override
{
m_dragging = false;
}
virtual void mouseMoveEvent (QMouseEvent *event) override
{
if (m_dragging) {
int new_value = m_base_value + m_scale_factor * (m_mouse_press_point.y() - event->y());
setValue(new_value);
}
}
private:
bool m_dragging;
QPoint m_mouse_press_point;
int m_base_value;
float m_scale_factor;
};
You'll probably want to adjust m_scale_factor
based on screen resolution or similar. 您可能希望根据屏幕分辨率或类似情况调整
m_scale_factor
。
If you don't want to subclass QDial
the same logic could be implemented with a stateful event filter. 如果您不想
QDial
则可以使用有状态事件过滤器实现相同的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.