QGraphicsItem questions about not receiving tab events

I. Background

Qt do with a similar mind mapping software, it is necessary to use QGraphicView framework, while the need to deal with keyboard input, but the input '\ t' strange phenomenon occurs.

Second, the problem

Custom GraphicsItem not receive keyPressEvent tab key. When the tab is pressed and released non-sequence of events is: view press event event => scene press => item press event => item release event => scene release event => view release event, the order is normal; and when you press the tab key, the sequence of events is: scene press event => view press event => scene press event => item release event => scene release event => view release, this order is odd, and the expected Different.

Third, the solution

Rewrite QGraphicsView and QGraphicsScene the event method, rewrite sceneEvent method of QGraphicsItem.

//....
class GraphicsScene : public QGraphicsScene{
	//....
protected:
    virtual bool event(QEvent *event)override{
        if(event->type() == QEvent::KeyPress){
            QKeyEvent *k = static_cast<QKeyEvent *>(event);
            if(k->key() == Qt::Key_Tab){
                this->keyPressEvent(k);
                return true;
            }
        }
        return QGraphicsScene::event(event);
    }
    //....
};

class GraphicsView : public QGraphicsView{
	//....
protected:
    virtual bool event(QEvent *event)override{
        if(event->type() == QEvent::KeyPress){
            QKeyEvent *k = static_cast<QKeyEvent *>(event);
            if(k->key() == Qt::Key_Tab){
                this->keyPressEvent(k);
                return true;
            }
        }
        return QGraphicsView::event(event);
    }
    //....
};

class GraphicsItem : public QGraphicsItem{
	//....
protected:
    virtual bool sceneEvent(QEvent* Event)override{
        if(Event->type() == QEvent::KeyPress){
            QKeyEvent *k = static_cast<QKeyEvent *>(Event);
            if(k->key() == Qt::Key_Tab){
                this->keyPressEvent(k);
                return true;
            }
        }   
        return QGraphicsItem::sceneEvent(Event);
    }
    //....
};

Fourth, the process

According to Qt documentation, QGraphicsView have found a way:

bool QGraphicsScene::focusNextPrevChild(bool next)

The method to do this when the tab key is pressed a number of operations, similar to the ordinary control when the transfer press tab will focus to the next control such operations, it is possible to determine the tab key is indeed a special treatment.

Look again at the Qt documentation for the event (https://doc.qt.io/archives/qt-4.8/eventsandfilters.html), you can see this description and the following codes:

These objects can reimplement QObject::event(), the general event handler, and either do their event handling before or after the usual handling, or they can replace the function completely. A very unusual widget that both interprets Tab and has an application-specific custom event might contain the following event() function:

bool MyWidget::event(QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Tab) {
            // special tab handling here
            return true;
        }
    } else if (event->type() == MyCustomEventType) {
        MyCustomEvent *myEvent = static_cast<MyCustomEvent *>(event);
        // custom event handling here
        return true;
    }

    return QWidget::event(event);
}

In other words, not just QGraphicsItem, seems to be all keyPressEvent, you want to deal with pressing the tab key events, you are likely to be rewritten event method.

Released five original articles · won praise 0 · Views 3088

Guess you like

Origin blog.csdn.net/NONElgh/article/details/104081796