除非我誤解你想,這應該是相當簡單的東西像什麼......
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;
};
您可能需要根據屏幕分辨率或類似的調整m_scale_factor
。
如果您不想繼承QDial
,則可以使用有狀態事件過濾器實現相同的邏輯。