如何使QLineEdit跟随光标来显示它的坐标 [英] How to make a QLineEdit follow the cursor to show me its coordinates

查看:1017
本文介绍了如何使QLineEdit跟随光标来显示它的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 QWidget 显示我的光标在图像上的坐标,我读的最好的方法是使用 QLineEdit ,但我没有找到如何使用它这样做。如何启动 QLineEdit 以及如何显示它,以便它跟随光标?
PS:我知道如何设置点坐标就可以了。
这是我怎么做:

I want my QWidget to show me the coordinates of my cursor when it's on the Image, I read that the best way is to use QLineEdit, but I didn't find how to use it to do so. How to initiate the QLineEdit and how to show it, in order that it follows the cursor ? PS : I know how to set the points coordinates on it. this is how i'm doing it :

void QImageWidget::mouseMoveEvent( QMouseEvent *event ){
   int x = event->pos( ).x();
   int y = event->pos( ).y();
   if( cursorLineEdit != NULL && cursorLineEdit->isEnabled( ) )
       cursorLineEdit->setText( QString( "[ %1 , %2 ]" ).arg( x ).arg( y ) );
}

mouseTracking已设为true:

the mouseTracking is already set true :

this->setMouseTracking(true);

谢谢!

编辑:cursorLineEdit QLineEdit我想显示,我需要在我的QWidget构造函数初始化它,但我不知道如何!

EDIT : cursorLineEdit is the QLineEdit i want to show, I need to initialize it on my QWidget Constructor, but i don't know how !

推荐答案

我找到一个解决方案,但首先我想告诉你,为什么我决定post我的答案在这里。我认为我的解决方案不高效,非常加载CPU,但当我运行vahancho的代码,我看到QToolTip加载CPU太(在我的计算机上两个解决方案可以加载CPU从0到3%)所以现在我认为我可以在这里回答并且您能够决定要使用什么。

I find one more solution, but first of all I want to tell you, why I decide post my answer here. I thought that my solution not efficient and very loads CPU, but when I run vahancho's code I saw that QToolTip loads CPU too (on my computer both solutions can load CPU from 0 to 3 percent) So now I think that I can post here answer and you be able to decide what you want to use.

想法:获取位置,创建透明像素图,绘制此像素图坐标,将此像素图设置为光标。此外,我们将使用一个bool变量(我们不会绘制像素图每一个mouseMoveEvent,我们将绘制它每秒(为效率))

Idea: get the position, create transparent pixmap, draw on this pixmap coordinates, set this pixmap as a cursor. Moreover, we will use one bool variable(we will not draw pixmap every mouseMoveEvent, we will draw it every second time (for efficiency))

bool showMustGoOn;//in header

showMustGoOn = false;//in constructor

void QImageWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(showMustGoOn)
    {
    const QPoint &p = event->pos();
    QPixmap px(50,10);
    px.fill(Qt::transparent);
    QPainter paint(&px);
    paint.drawText(10,10,QString("%1,%2").arg(p.x()).arg(p.y()));
    setCursor(QCursor(px));
    showMustGoOn = false;
    }
    else
    {
        showMustGoOn = true;
    }
}

如果你想使用它,显示光标。另一个优点是pixmap是透明的,所以这个光标不会关闭另一个区域(你只看到数字,所有其他都是透明的,但工具提示关闭)

If you want use it, you can draw something another to show the cursor. One more advanteges is that pixmap is transparent, so this cursor not close another area(you see just numbers, all another is transparent, but toolTip close)

有一些类似的东西,我认为它更有效率,但在Qwt的源代码中搜索所需的代码可以非常长,复杂。

Finally, in Qwt there is something similar and I think it more efficient, but searching of needed code in the source code of Qwt can be very long and complicate.

这篇关于如何使QLineEdit跟随光标来显示它的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆