qt 自定义旋钮
时间: 2023-07-02 12:10:06 浏览: 286
qt 自定义的按钮
在 Qt 中,可以通过自定义绘制实现一个旋钮。以下是一个简单的例子:
1. 定义一个自定义旋钮控件类,继承自 QWidget。
2. 在控件类中定义旋钮的属性,如当前值、最小值、最大值、步长等。
3. 重写 paintEvent() 函数,在函数中使用 QPainter 绘制旋钮。
4. 重写 mousePressEvent() 和 mouseMoveEvent() 函数,实现旋钮的交互操作,如拖动旋钮改变值。
以下是一个简单的自定义旋钮控件的示例代码:
```cpp
class Knob : public QWidget
{
Q_OBJECT
public:
explicit Knob(QWidget *parent = nullptr);
void setValue(int value);
int value() const;
void setMinimum(int minimum);
int minimum() const;
void setMaximum(int maximum);
int maximum() const;
void setSingleStep(int step);
int singleStep() const;
signals:
void valueChanged(int value);
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private:
int m_value;
int m_minimum;
int m_maximum;
int m_singleStep;
QPoint m_center;
int m_radius;
int m_angle;
};
Knob::Knob(QWidget *parent)
: QWidget(parent)
, m_value(0)
, m_minimum(0)
, m_maximum(100)
, m_singleStep(1)
, m_center(width() / 2, height() / 2)
, m_radius(qMin(width(), height()) / 2 - 10)
, m_angle(0)
{
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
}
void Knob::setValue(int value)
{
if (value < m_minimum)
value = m_minimum;
if (value > m_maximum)
value = m_maximum;
if (value != m_value) {
m_value = value;
emit valueChanged(m_value);
update();
}
}
int Knob::value() const
{
return m_value;
}
void Knob::setMinimum(int minimum)
{
m_minimum = minimum;
}
int Knob::minimum() const
{
return m_minimum;
}
void Knob::setMaximum(int maximum)
{
m_maximum = maximum;
}
int Knob::maximum() const
{
return m_maximum;
}
void Knob::setSingleStep(int step)
{
m_singleStep = step;
}
int Knob::singleStep() const
{
return m_singleStep;
}
void Knob::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// 绘制外圆
painter.save();
painter.translate(m_center);
painter.setPen(QPen(Qt::black, 2));
painter.drawEllipse(QPoint(0, 0), m_radius, m_radius);
painter.restore();
// 绘制刻度线
painter.save();
painter.translate(m_center);
painter.rotate(-120);
for (int i = 0; i <= 20; ++i) {
if (i % 2 == 0) {
painter.drawLine(0, m_radius - 10, 0, m_radius - 20);
painter.drawText(-10, m_radius - 22, QString::number(i * 5));
} else {
painter.drawLine(0, m_radius - 15, 0, m_radius - 20);
}
painter.rotate(6);
}
painter.restore();
// 绘制指针
painter.save();
painter.translate(m_center);
painter.rotate(-120 + m_angle);
painter.setPen(QPen(Qt::red, 3));
painter.drawLine(0, 0, m_radius - 30, 0);
painter.restore();
}
void Knob::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_center = QPoint(width() / 2, height() / 2);
m_radius = qMin(width(), height()) / 2 - 10;
m_angle = qRound(qRadiansToDegrees(qAtan2(event->y() - m_center.y(), event->x() - m_center.x())));
}
}
void Knob::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int newAngle = qRound(qRadiansToDegrees(qAtan2(event->y() - m_center.y(), event->x() - m_center.x())));
int delta = newAngle - m_angle;
if (delta > 180)
delta -= 360;
if (delta < -180)
delta += 360;
setValue(m_value + delta / 2);
m_angle = newAngle;
}
}
```
在使用时,可以像使用其他控件一样添加到窗口中,并连接 valueChanged() 信号以处理旋钮的值改变事件。
```cpp
Knob *knob = new Knob(this);
knob->setGeometry(50, 50, 100, 100);
connect(knob, &Knob::valueChanged, [=](int value) {
qDebug() << "value: " << value;
});
```
阅读全文