custom QQuickPaintedItem in SwipeView cannot accept key event
-
As you can see code is really simple. I custom a QQuickPaintedItem class named CustomItem.here is the code.
class CustomItem : public QQuickPaintedItem { Q_OBJECT public: CustomItem( QQuickItem* parent = nullptr ) { setFlag( QQuickItem::ItemIsFocusScope, true ); setFocus(true); } void paint( QPainter* painter ) { painter->setBrush(Qt::cyan); painter->setPen(Qt::darkCyan); painter->drawRect(0, 0, 100,100); painter->setBrush(Qt::NoBrush); painter->setPen(Qt::darkGreen); painter->drawRect(40, 40, 100, 100); } protected: virtual void keyPressEvent(QKeyEvent* event) { qDebug() << event->key(); } private slots: }; qmlRegisterType<CustomItem>("CustomItem", 1, 0, "CustomItem");
CustomItem in Item can accept key event
ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("KeyBoard Input For Custom QQuickPaintedItem") Item { id: mainView anchors.fill: parent CustomItem { anchors.fill: parent } } }
but, CustomItem in SwipeView cannot accept key event
ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("KeyBoard Input For Custom QQuickPaintedItem") SwipeView { id: swipeView anchors.fill: parent currentIndex: tabBar.currentIndex interactive: false CustomItem { } Page { Label { text: qsTr("custom QQuickPaintedItem") anchors.centerIn: parent } } } footer: TabBar { id: tabBar currentIndex: swipeView.currentIndex TabButton { text: qsTr("View") } TabButton { text: qsTr("Info") } } }
what's wrong
-
Need to set focus:true for swipeview. It should work.
-
Need to set focus:true for swipeview. It should work.
@dheerendra Is the issue related to the version? I'm using Qt 5.12.10.
-
I'm using Qt 5.12.10.
Is upgrading to Qt 5.15 or 6 possible? If so, does that fix the issue?
CustomItem in SwipeView cannot accept key event
Does the same thing happen when deriving from QQuickItem or using a Qt provided element such as Item?
What item has focus when the SwipeView is displaying CustomItem? Use Window.activeFocusItem and its change signal to find out.
A visual indicator can be created by reparenting a Rectangle. Eg:
Window { Rectangle { id: focusTracer border.width: 1 border.color: "red" color: "transparent" anchors.fill: parent parent: Window.activeFocusItem } ... }