LoginSignup
3
3

More than 5 years have passed since last update.

Qt5の異常なドロップイベントを無視する

Last updated at Posted at 2016-10-24

:warning: この障害(QTBUG-46287)は修正済みです。

Qt5には、モーダルウィジェットが存在してもメインウィンドウがドロップイベントを受け付けてしまうというバグがあります。

回避するには、異常なDragEnterイベントを止めるイベントフィルターを作り、QApplicationへインストールします。

  • アクティブなモーダルウィジェットが存在するとき、DragEnterイベントを無視する
  • ただし、 アクティブなモーダルウィジェットに対して発生したものは受理する

DragEnterを無視するとドロップもできなくなり、マウスカーソルがドロップを受け付けるアイコンになりません。

実装例は以下の通りです。

class DragEventEater : public QObject
{
public:
    DragEventEater(QObject* parent): QObject(parent) {}
protected:
    bool eventFilter(QObject *obj, QEvent *event)
    {
        if (event->type() == QEvent::DragEnter)
        {
            if (QObject* modalWidget = QApplication::activeModalWidget())
            {
                for (; obj; obj = obj->parent())
                {
                    if (obj == modalWidget)
                    {
                        return false;
                    }
                };
                return true;
            }
        }
        return false;
    }
};
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3