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. C++ and QML Integration
Forum Updated to NodeBB v4.3 + New Features

C++ and QML Integration

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.7k Views 2 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.
  • A Offline
    A Offline
    AhmedYossef
    wrote on last edited by
    #1

    Hello,

    Currently i am building a game using Qt Quick and i am using QQuickView and QQuickItem to create instances of my objects from QML files, for example i have a game character "image in qml file" and i am loading it using QComponent and QQuickItem and then i add it to the parent "QQuickView", now i want this character to respond to my keyboard events.

    p3c0P 1 Reply Last reply
    0
    • A AhmedYossef

      Hello,

      Currently i am building a game using Qt Quick and i am using QQuickView and QQuickItem to create instances of my objects from QML files, for example i have a game character "image in qml file" and i am loading it using QComponent and QQuickItem and then i add it to the parent "QQuickView", now i want this character to respond to my keyboard events.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @AhmedYossef and Welcome to Qt Devnet.

      By "using QQuickItem" I'm assuming you have subclassed QQuickItem. If so, then in that case you can also re-implement keyPressEvent and keyReleaseEvent. You may need to set focus to true.

      157

      A 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @AhmedYossef and Welcome to Qt Devnet.

        By "using QQuickItem" I'm assuming you have subclassed QQuickItem. If so, then in that case you can also re-implement keyPressEvent and keyReleaseEvent. You may need to set focus to true.

        A Offline
        A Offline
        AhmedYossef
        wrote on last edited by
        #3

        Thanks for the reply, i did that i sub classed QQuickItem, and in the constructor of the character i created the QQMLComponent and the QQuickItem, then i add the QQuickItem to the parent which is the QQuickView, then i set focus and i set initial X and Y for the character, also i implemented the keyPressEvent, but still nothing happens the character appears in the default X and Y i set before but when i press the key it doesn't go to the event

        p3c0P 1 Reply Last reply
        0
        • A AhmedYossef

          Thanks for the reply, i did that i sub classed QQuickItem, and in the constructor of the character i created the QQMLComponent and the QQuickItem, then i add the QQuickItem to the parent which is the QQuickView, then i set focus and i set initial X and Y for the character, also i implemented the keyPressEvent, but still nothing happens the character appears in the default X and Y i set before but when i press the key it doesn't go to the event

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @AhmedYossef Can you post some code ? Did you set focus ?

          157

          A 2 Replies Last reply
          0
          • p3c0P p3c0

            @AhmedYossef Can you post some code ? Did you set focus ?

            A Offline
            A Offline
            AhmedYossef
            wrote on last edited by
            #5

            @p3c0
            Here is what i do in the constructor of the character
            QQmlComponent component(view->engine(), QUrl(QStringLiteral("qrc:/PlayerTank.qml")));
            tankItem = qobject_cast<QQuickItem*>(component.create());
            tankItem->setParentItem(view->rootObject());

            tankItem->setX(100);
            tankItem->setY(100);
            tankItem->setFocus(true);
            

            tabkItem is a QQuickItem

            1 Reply Last reply
            0
            • p3c0P p3c0

              @AhmedYossef Can you post some code ? Did you set focus ?

              A Offline
              A Offline
              AhmedYossef
              wrote on last edited by
              #6

              @p3c0
              Got it to work finally, i implemented the Keypressevents for the class that subsclassed the QQuickView and it works i can now move the characters

              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