Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. custom QQuickPaintedItem in SwipeView cannot accept key event
Qt 6.11 is out! See what's new in the release blog

custom QQuickPaintedItem in SwipeView cannot accept key event

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zero-one
    wrote on last edited by Zero-one
    #1

    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

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zero-one
      wrote on last edited by Zero-one
      #6

      I'm sure it is retalted to the version. I set swipeview's focus to true and upgradd to Qt 5.14, it work. (in Qt 5.12 not work). @jeremy_k @dheerendra

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by
        #2

        Need to set focus:true for swipeview. It should work.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        Z 1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zero-one
          wrote on last edited by
          #3

          I set swipeview's focus to true, it not work.

          1 Reply Last reply
          0
          • dheerendraD dheerendra

            Need to set focus:true for swipeview. It should work.

            Z Offline
            Z Offline
            Zero-one
            wrote on last edited by
            #4

            @dheerendra Is the issue related to the version? I'm using Qt 5.12.10.

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #5

              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
                  }
                  ...
              }
              

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zero-one
                wrote on last edited by Zero-one
                #6

                I'm sure it is retalted to the version. I set swipeview's focus to true and upgradd to Qt 5.14, it work. (in Qt 5.12 not work). @jeremy_k @dheerendra

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved