在Qt中捕获拖动取消事件 [英] Catching drag cancel event in Qt

查看:232
本文介绍了在Qt中捕获拖动取消事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Qt中捕获应用程序范围事件时遇到一些困难。



我的目的是捕获某些GUI应用程序中的每个用户操作。当然,我已经改写了 QApplication :: notify 处理程序并处理不同类型的应用程序范围事件。像这样:

  bool CoolApplication :: notify(QObject * obj,QEvent * e)
{
switch(e-> type()){
case QEvent :: MouseButtonPress:
case QEvent :: MouseButtonRelease:
//处理鼠标
break;

case QEvent :: Wheel:
//处理轮
break;

case QEvent :: KeyPress:
case QEvent :: KeyRelease:
//处理键盘等
break;
默认值:
break;
}

return QApplication :: notify(obj,e);
}

这样工作得很好,直到用户开始拖放操作。鼠标按下事件处理得很好,它在我的处理程序,捕捉生活是美丽的。当我尝试在Windows上捕获鼠标释放事件时,问题就开始了。



当输入拖动操作时,鼠标和键盘事件不再由QApplication :: notify处理。它是可能的catch事件,当它是成功的(我可以添加 case QEvent :: Drop 分支到我的处理程序),但如何捕获忽略删除或取消删除与Esc ?似乎不可能直接做,也许我可以捕获拖动事件循环终止事件或类似的东西?



编辑:
一次又一次的关注我需要捕获应用程序范围的事件,而不是

请尝试此

  code> bool QtCoreApplication :: notify(QObject * obj,QEvent * e)
{
switch(e-> type()){
case QEvent :: QDropEvent:
QDropEvent * dropEvent =(QDropEvent *)e;
dropEvent-> keyboardModifiers(); // keys pressed
dropEvent-> dropAction(); // drop result
break;

return QApplication :: notify(obj,e);
}


I'm experiencing some difficulties with catching application-scope events in Qt.

My purpose is to catch every user action in some GUI application. Of course I've overrided QApplication::notify handler and processing different types of application-scope events there. Something like that:

bool CoolApplication::notify(QObject *obj, QEvent *e)
{
    switch (e->type()) {
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
        // Handle mouse
        break;

    case QEvent::Wheel:
        // Handle wheel
        break;

    case QEvent::KeyPress:
    case QEvent::KeyRelease:
        // Handle keyboard and so on
        break;
    default:
        break;
    }

    return QApplication::notify(obj, e);
}

That works pretty well until user starts drag-and-drop operation. Mouse press event is handled well, it is catched in my handler, life is beautiful there. The problems start when I try to catch mouse release event on Windows.

When entering drag operation mouse and keyboard events are no longer handled by QApplication::notify. It is possible to catch drop event when it was successfull (I can add case QEvent::Drop branch into my handler), but how to catch ignored drop or canceling drop with Esc? It seems impossible to be done directly, maybe I can catch drag event loop termination event or something like that? Or even maybe someone knows a direct way to do it?

Edit: Once more turning the attention that I need to catch application-scope event, not the widget one

解决方案

Please, try this

bool QtCoreApplication::notify(QObject *obj, QEvent *e)
    {
        switch (e->type()) {
        case QEvent::QDropEvent:
            QDropEvent* dropEvent = (QDropEvent*)e;
            dropEvent->keyboardModifiers(); // keys pressed
            dropEvent->dropAction(); // Drop result
        break;

        return QApplication::notify(obj, e);
    }

这篇关于在Qt中捕获拖动取消事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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