已解决
qt中怎么在鼠标停留的位置上显示该点的坐标位置
来自网友在路上 151851提问 提问时间:2023-10-26 19:09:46阅读次数: 51
最佳答案 问答题库518位专家为你答疑解惑
需要重写控件的mouseMoveEvent方法。
1、自定义一个QLabel控件,然后重写QLabel的mouseMoveEvent
customlabel.h#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>class CustomLabel : public QLabel
{Q_OBJECT
public:explicit CustomLabel(QWidget *parent = nullptr);void setPiture(QString path);void paintLine(QPoint startPoint, QPoint endPoint);protected:void mouseMoveEvent(QMouseEvent* event) override;
signals:private:QHBoxLayout* m_layout;QList<QPoint> m_pointList;
};
customlabel.cpp#include "customlabel.h"
#include <QMouseEvent>
#include <QToolTip>CustomLabel::CustomLabel(QWidget *parent): QLabel{parent}
{setMouseTracking(true);show();
}void CustomLabel::mouseMoveEvent(QMouseEvent *event)
{QPoint pos = event->pos();QString positionString = QString("X: %1, Y: %2").arg(pos.x()).arg(pos.y());// 设置工具提示文本QToolTip::showText(event->globalPos(), positionString, this);QColor color(Qt::red);QPalette palette;palette.setColor(QPalette::Text,color);QToolTip::setPalette(palette);
}
2、在主界面widget中使用CustomLabel。
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);CustomLabel* label = new CustomLabel(this);ui->verticalLayout_2->addWidget(label);
}
3、看效果
鼠标箭头处显示坐标
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"qt中怎么在鼠标停留的位置上显示该点的坐标位置":http://eshow365.cn/6-25353-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!