DSCAnalysisTool/YT_TANGOUS_Release/dragqcptext.h
2025-03-06 15:25:37 +08:00

43 lines
1.2 KiB
C++

#ifndef DRAGQCPTEXT_H
#define DRAGQCPTEXT_H
#include "qcustomplot.h"
class DraggableText : public QCPItemText {
public:
DraggableText(QCustomPlot *parentPlot) : QCPItemText(parentPlot) {
///setClipToAxis(false); // 允许拖出轴范围
}
protected:
QPointF clickPos; // 记录鼠标点击位置
bool isDragging = false; // 是否正在拖动
void mousePressEvent(QMouseEvent *event, const QPointF &pos) {
QCPItemText::mousePressEvent(event, pos);
if (event->button() == Qt::RightButton) {
clickPos = pos; // 记录点击位置
}
}
void mouseMoveEvent(QMouseEvent *event, const QPointF &pos) override {
QCPItemText::mouseMoveEvent(event, pos);
if (isDragging) {
double dx = pos.x() - clickPos.x();
double dy = pos.y() - clickPos.y();
position->setCoords(position->coords().x() + dx, position->coords().y() + dy);
}
}
void mouseReleaseEvent(QMouseEvent *event, const QPointF &pos) override {
QCPItemText::mouseReleaseEvent(event, pos);
if (event->button() == Qt::RightButton) {
isDragging = false;
}
}
};
#endif // DRAGQCPTEXT_H