获取 QLabel 中鼠标单击的位置

发布于 2024-10-06 04:28:21 字数 632 浏览 4 评论 0原文

在 QLabel 中获取 mousePressedEventpos 的最佳(最简单)方法是什么? (或者基本上只是获取相对于 QLabel 小部件的鼠标单击位置)

编辑

我尝试了 Frank 建议的方式:

bool MainWindow::eventFilter(QObject *someOb, QEvent *ev)
{
if(someOb == ui->label && ev->type() == QEvent::MouseButtonPress)
{
    QMouseEvent *me = static_cast<QMouseEvent *>(ev);
    QPoint coordinates = me->pos();
    //do stuff
    return true;
}
else return false;
}

但是,我收到编译错误 invalid static_cast from type 'QEvent *' 以在我尝试声明 me 的行上键入“const QMouseEvent*”。你知道我在这里做错了什么吗?

What is the best (as in simplest) way to obtain the pos of a mousePressedEvent in a QLabel? (Or basically just obtain the location of a mouse click relative to a QLabel widget)

EDIT

I tried what Frank suggested in this way:

bool MainWindow::eventFilter(QObject *someOb, QEvent *ev)
{
if(someOb == ui->label && ev->type() == QEvent::MouseButtonPress)
{
    QMouseEvent *me = static_cast<QMouseEvent *>(ev);
    QPoint coordinates = me->pos();
    //do stuff
    return true;
}
else return false;
}

However, I receive the compile error invalid static_cast from type 'QEvent*' to type 'const QMouseEvent*' on the line where I try to declare me. Any ideas what I'm doing wrong here?

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

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

发布评论

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

评论(2

许一世地老天荒 2024-10-13 04:28:21

您可以子类化 QLabel 并重新实现 mousePressEvent(QMouseEvent*)。或者使用事件过滤器:

bool OneOfMyClasses::eventFilter( QObject* watched, QEvent* event ) {
    if ( watched != label )
        return false;
    if ( event->type() != QEvent::MouseButtonPress )
        return false;
    const QMouseEvent* const me = static_cast<const QMouseEvent*>( event );
    //might want to check the buttons here
    const QPoint p = me->pos(); //...or ->globalPos();
    ...
    return false;
}


label->installEventFilter( watcher ); // watcher is the OneOfMyClasses instance supposed to do the filtering.

事件过滤的优点是更灵活并且不需要子类化。但是,如果您无论如何都需要因接收到的事件而产生自定义行为,或者已经有一个子类,那么重新实现 fooEvent() 会更直接。

You could subclass QLabel and reimplement mousePressEvent(QMouseEvent*). Or use an event filter:

bool OneOfMyClasses::eventFilter( QObject* watched, QEvent* event ) {
    if ( watched != label )
        return false;
    if ( event->type() != QEvent::MouseButtonPress )
        return false;
    const QMouseEvent* const me = static_cast<const QMouseEvent*>( event );
    //might want to check the buttons here
    const QPoint p = me->pos(); //...or ->globalPos();
    ...
    return false;
}


label->installEventFilter( watcher ); // watcher is the OneOfMyClasses instance supposed to do the filtering.

The advantage of event filtering is that is more flexible and doesn't require subclassing. But if you need custom behavior as a result of the received event anyway or already have a subclass, its more straightforward to just reimplement fooEvent().

盗梦空间 2024-10-13 04:28:21

我有同样的问题

无效的 static_cast...

我只是忘记包含标头:#include "qevent.h"

现在一切正常。

I had the same problem

invalid static_cast...

I just forgot to include the header: #include "qevent.h"

Now everything is working well.

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