利用qt绘制一个有刻度的平面圆形图,然后在平面圆形图里有两条圆心出发的指针为一条黄线和一条红线,红线指针随鼠标移动而移动,黄线指针在鼠标点击时发生移动,用三角函数求平面上两条指针的平面夹角度数,并在ui界面显示每次变化的角度
时间: 2024-03-11 09:49:44 浏览: 124
好的,这是一个比较复杂的问题,需要分步骤来实现。
首先,我们需要绘制一个有刻度的平面圆形图。可以使用Qt的绘图功能,绘制一个圆形,并在圆周上绘制刻度线。代码如下:
```cpp
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.translate(width() / 2, height() / 2); // 将圆心移到窗口中心
int r = qMin(width(), height()) / 2 - 20; // 圆形半径
painter.drawEllipse(-r, -r, 2 * r, 2 * r); // 绘制圆形
int n = 36; // 刻度线数量
for (int i = 0; i < n; ++i) {
painter.drawLine(0, -r, 0, -r + 10); // 绘制刻度线
painter.rotate(360 / n); // 旋转画布
}
}
```
接下来,我们需要绘制两条指针。一条红线指针,一条黄线指针。红线指针随鼠标移动而移动,黄线指针在鼠标点击时发生移动。代码如下:
```cpp
void MyWidget::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
QPointF pos = mapFromGlobal(QCursor::pos()); // 鼠标点击位置
m_yellowAngle = angle(pos); // 计算黄线指针的初始角度
update(); // 更新界面
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
QPointF pos = mapFromGlobal(QCursor::pos()); // 鼠标当前位置
m_redAngle = angle(pos); // 计算红线指针的角度
update(); // 更新界面
}
void MyWidget::paintEvent(QPaintEvent *event)
{
// ...
// 绘制红线指针
painter.save();
painter.rotate(m_redAngle);
painter.setPen(Qt::red);
painter.drawLine(0, 0, 0, -r * 0.8);
painter.restore();
// 绘制黄线指针
painter.save();
painter.rotate(m_yellowAngle);
painter.setPen(Qt::yellow);
painter.drawLine(0, 0, 0, -r * 0.6);
painter.restore();
// ...
}
double MyWidget::angle(const QPointF &pos)
{
QPointF center(width() / 2, height() / 2);
QPointF vec = pos - center;
double angle = qRadiansToDegrees(qAtan2(vec.y(), vec.x()));
return angle;
}
```
最后,我们需要用三角函数求平面上两条指针的平面夹角度数,并在ui界面显示每次变化的角度。代码如下:
```cpp
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
// ...
double angle = m_redAngle - m_yellowAngle; // 计算夹角
while (angle < 0) angle += 360;
while (angle > 360) angle -= 360;
m_angleLabel->setText(QString::number(angle)); // 显示角度
// ...
}
```
完整代码如下:
```cpp
#include <QtWidgets>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent)
, m_redAngle(0)
, m_yellowAngle(0)
{
setFixedSize(400, 400);
m_angleLabel = new QLabel(this);
m_angleLabel->move(width() / 2, height() - 30);
m_angleLabel->setAlignment(Qt::AlignHCenter);
m_angleLabel->setFixedWidth(100);
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.translate(width() / 2, height() / 2);
int r = qMin(width(), height()) / 2 - 20;
painter.drawEllipse(-r, -r, 2 * r, 2 * r);
int n = 36;
for (int i = 0; i < n; ++i) {
painter.drawLine(0, -r, 0, -r + 10);
painter.rotate(360 / n);
}
painter.save();
painter.rotate(m_redAngle);
painter.setPen(Qt::red);
painter.drawLine(0, 0, 0, -r * 0.8);
painter.restore();
painter.save();
painter.rotate(m_yellowAngle);
painter.setPen(Qt::yellow);
painter.drawLine(0, 0, 0, -r * 0.6);
painter.restore();
}
void mousePressEvent(QMouseEvent *event) override
{
Q_UNUSED(event);
QPointF pos = mapFromGlobal(QCursor::pos());
m_yellowAngle = angle(pos);
update();
}
void mouseMoveEvent(QMouseEvent *event) override
{
QPointF pos = mapFromGlobal(QCursor::pos());
m_redAngle = angle(pos);
update();
double angle = m_redAngle - m_yellowAngle;
while (angle < 0) angle += 360;
while (angle > 360) angle -= 360;
m_angleLabel->setText(QString::number(angle));
}
double angle(const QPointF &pos)
{
QPointF center(width() / 2, height() / 2);
QPointF vec = pos - center;
double angle = qRadiansToDegrees(qAtan2(vec.y(), vec.x()));
return angle;
}
private:
double m_redAngle;
double m_yellowAngle;
QLabel *m_angleLabel;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
```
阅读全文