获取鼠标实时坐标

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#top_div{
position:absolute;
width:100PX;
height:50px;
border:1px solid #00F;
text-align:center;
color:blue;
}
</style>
<script type="text/javascript" src="jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
function initMouseMove(){
document.onmousemove = function (e){
x = document.documentElement.scrollLeft+e.clientX;
y = document.documentElement.scrollTop+e.clientY;
$("#top_div").html("当前坐标 X:"+x+"Y:"+y);
$("#top_div").css("left",x);
$("#top_div").css("top",y);
}}

</script>
</head>
<body onload="initMouseMove()">
<div id="top_div"></div>
</body>
</html>
在QT中使用QCustomPlot获取鼠标实时坐标,可以通过继承`QCustomPlot`类并重写`mouseMoveEvent`方法实现。下面是具体步骤和示例代码: 首先,创建一个继承自`QCustomPlot`的自定义类,如`MFMCustomPlot`。在该类的`mouseMoveEvent`方法中,获取鼠标在窗口中的位置,然后将其转换为坐标系中的位置,最后使用`QToolTip`显示坐标信息。 示例代码如下: ```cpp #include <QCustomPlot> #include <QMouseEvent> #include <QToolTip> #include <QString> class MFMCustomPlot : public QCustomPlot { Q_OBJECT public: explicit MFMCustomPlot(QWidget *parent = nullptr) : QCustomPlot(parent) {} protected: void mouseMoveEvent(QMouseEvent *e) override { /* 获取光标位置 */ int x_pos = e->pos().x(); int y_pos = e->pos().y(); /* 转化为坐标系位置 */ double xv = this->xAxis->pixelToCoord(x_pos); double yv = this->yAxis->pixelToCoord(y_pos); QString str; str = QString("\nx:%1\ny:%2").arg(QString::asprintf("%.3f", xv)).arg(QString::asprintf("%.3f", yv)); QToolTip::showText(cursor().pos(), str, this); } }; ``` 在主窗口中使用自定义的`MFMCustomPlot`类: ```cpp #include <QMainWindow> #include <QVBoxLayout> #include "mfmcustomplot.h" class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { // 创建MFMCustomPlot控件,并布局到窗体中 MFMCustomPlot *customPlot = new MFMCustomPlot(this); QVBoxLayout *layout = new QVBoxLayout; QWidget *centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget); layout->addWidget(customPlot); } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值