在qt中获取标签的鼠标点击位置

发布于 2024-10-06 03:02:25 字数 750 浏览 3 评论 0原文

我用谷歌搜索了一下,发现 这个论坛帖子,其中OP似乎遇到了我遇到的确切问题。问题是,我如何继承 QLabel 并重新实现 mousepressed 事件?我猜它会是这样的:

class CustomLabel : public QLabel
{
public:
    //what about the constructors?
    void mousePressEvent ( QMouseEvent * ev );
}

void CustomLabel::mousePressEvent ( QMouseEvent * ev )
{
    QPoint = ev->pos();
    //I want to have another function get the event position.
    //How would I achieve this? It's void!
    //Is there perhaps some way to set up a signal and slot with the position?
}

在我成功创建一个 CustomLabel 类之后,我如何才能将它放入设计视图中?

I googled around and found this forum thread in which the OP seems to have had the exact problem I am having. The question is, how would I inherit from QLabel and reimplement the mousepressed event? I'm guessing it would be something like this:

class CustomLabel : public QLabel
{
public:
    //what about the constructors?
    void mousePressEvent ( QMouseEvent * ev );
}

void CustomLabel::mousePressEvent ( QMouseEvent * ev )
{
    QPoint = ev->pos();
    //I want to have another function get the event position.
    //How would I achieve this? It's void!
    //Is there perhaps some way to set up a signal and slot with the position?
}

And after I have successfully created a CustomLabel class, how would I be able to put it in design view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长安忆 2024-10-13 03:02:25

是的,您可以在 CustomLabel 类上设置信号,并让覆盖版本的 mousePressEvent 发出该信号。即

class CustomLabel : public QLabel
{
Q_OBJECT
signals:
    void mousePressed( const QPoint& );

public:
    CustomLabel( QWidget* parent = 0, Qt::WindowFlags f = 0 );
    CustomLabel( const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0 );

    void mousePressEvent( QMouseEvent* ev );
};

void CustomLabel::mousePressEvent( QMouseEvent* ev )
{
    const QPoint p = ev->pos();
    emit mousePressed( p );
}

CustomLabel::CustomLabel( QWidget * parent, Qt::WindowFlags f )
    : QLabel( parent, f ) {}

CustomLabel::CustomLabel( const QString& text, QWidget* parent, Qt::WindowFlags f )
    : QLabel( text, parent, f ) {}

构造函数只是模仿基本 QLabel 的构造函数,因此只需将它们的参数直接传递给相应的基本构造函数。

Yes, you can set up a signal on your CustomLabel class and have your overridden version of mousePressEvent emit it. i.e.

class CustomLabel : public QLabel
{
Q_OBJECT
signals:
    void mousePressed( const QPoint& );

public:
    CustomLabel( QWidget* parent = 0, Qt::WindowFlags f = 0 );
    CustomLabel( const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0 );

    void mousePressEvent( QMouseEvent* ev );
};

void CustomLabel::mousePressEvent( QMouseEvent* ev )
{
    const QPoint p = ev->pos();
    emit mousePressed( p );
}

CustomLabel::CustomLabel( QWidget * parent, Qt::WindowFlags f )
    : QLabel( parent, f ) {}

CustomLabel::CustomLabel( const QString& text, QWidget* parent, Qt::WindowFlags f )
    : QLabel( text, parent, f ) {}

The constructors just mimic those of the base QLabel and therefore simply pass their arguments straight on to the corresponding base constructors.

无尽的现实 2024-10-13 03:02:25

就像这样:D

void CustomLabel::mousePressEvent(QMouseEvent *ev) 
{
QString x = QString::number(ev->x());
QString y = QString::number(ev->y());
qDebug() << x << "," << y;
}

just like this :D

void CustomLabel::mousePressEvent(QMouseEvent *ev) 
{
QString x = QString::number(ev->x());
QString y = QString::number(ev->y());
qDebug() << x << "," << y;
}
染火枫林 2024-10-13 03:02:25

是我一个人的问题,还是 QMouseEvent 没有提供您需要的信息?

int QMouseEvent::x () const

返回鼠标光标相对于接收事件的小部件的 x 位置。

另请参见 y() 和 pos()。

int QMouseEvent::y () const

返回鼠标光标相对于接收事件的小部件的 y 位置。

另请参见 x() 和 pos()。

参考:http://doc.qt.nokia.com/4.7.old /qmouseevent.html#x

Is it just me, or doesn't QMouseEvent already give the information you need?

int QMouseEvent::x () const

Returns the x position of the mouse cursor, relative to the widget that received the event.

See also y() and pos().

int QMouseEvent::y () const

Returns the y position of the mouse cursor, relative to the widget that received the event.

See also x() and pos().

Ref: http://doc.qt.nokia.com/4.7.old/qmouseevent.html#x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文