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. Implementing an action class in C++ for a QML app
Forum Updated to NodeBB v4.3 + New Features

Implementing an action class in C++ for a QML app

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 2 Posters 1.2k 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.
  • iwoitheI Offline
    iwoitheI Offline
    iwoithe
    wrote on last edited by
    #1

    I am trying to make an Action class for my C++/QML project. I wish to implement this action in C++ and register it as a context property. I am stuck however at creating this class. I have tried to use QAction but with no success. What is the best way to implement this?

    Thanks

    raven-worxR 1 Reply Last reply
    0
    • iwoitheI iwoithe

      I am trying to make an Action class for my C++/QML project. I wish to implement this action in C++ and register it as a context property. I am stuck however at creating this class. I have tried to use QAction but with no success. What is the best way to implement this?

      Thanks

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @iwoithe
      what are your exact requirements? QAction supports different scopes, shortcuts etc.
      Instead of a context property i suggest you create a singleton type or a "normal" element which can be instantiated multiple times.

      Also did you already see the available Action qml type?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • iwoitheI Offline
        iwoitheI Offline
        iwoithe
        wrote on last edited by
        #3

        QAction supports different scopes, shortcuts etc.

        I also read that QAction is for QtWidgets apps which is why I didn't keep trying it. Is that correct?

        Also did you already see the available Action qml type?

        I did take a brief look at the Action QML type, but it doesn't work for what I need to use it for (i.e. use it in C++).

        raven-worxR 1 Reply Last reply
        0
        • iwoitheI iwoithe

          QAction supports different scopes, shortcuts etc.

          I also read that QAction is for QtWidgets apps which is why I didn't keep trying it. Is that correct?

          Also did you already see the available Action qml type?

          I did take a brief look at the Action QML type, but it doesn't work for what I need to use it for (i.e. use it in C++).

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @iwoithe said in Implementing an action class in C++ for a QML app:

          Is that correct?

          yes

          but it doesn't work for what I need to use it for (i.e. use it in C++).

          since its a QObject you can use it in C++.
          You can write/read properties and call its methods using the meta-object system (which every QObject has)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • iwoitheI Offline
            iwoitheI Offline
            iwoithe
            wrote on last edited by
            #5

            since its a QObject you can use it in C++.

            Could you please provide a code sample of how to use the QML action in C++?

            raven-worxR 1 Reply Last reply
            0
            • iwoitheI iwoithe

              since its a QObject you can use it in C++.

              Could you please provide a code sample of how to use the QML action in C++?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @iwoithe
              Especially this section, but its worth reading the whole topic:
              https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • iwoitheI Offline
                iwoitheI Offline
                iwoithe
                wrote on last edited by
                #7

                I have a base implementation of the action controller (the repo is here). I ended up using a struct for the Action. My problem now is that QAction is not a registered QML type. Should I be using QAction at all? How should I adjust the code so that the Action QML type can be used instead of QAction?

                Also, should I really use the QML Action type?

                Warning: Although it is possible to access QML objects from C++ and manipulate them, it is not the recommended approach, except for testing and prototyping purposes.

                (from here)

                raven-worxR 1 Reply Last reply
                0
                • iwoitheI iwoithe

                  I have a base implementation of the action controller (the repo is here). I ended up using a struct for the Action. My problem now is that QAction is not a registered QML type. Should I be using QAction at all? How should I adjust the code so that the Action QML type can be used instead of QAction?

                  Also, should I really use the QML Action type?

                  Warning: Although it is possible to access QML objects from C++ and manipulate them, it is not the recommended approach, except for testing and prototyping purposes.

                  (from here)

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @iwoithe
                  QAction is for QtWidgets applications and doesn not integrate well with QML.

                  As long as you only set properties and call invokable methods of the objects you are fine, since its the same as wou would do it from QML. This statement rather refers to other manipulations you could do to a QObject (like setting a different parent and so on)
                  If so than the Shortcut qml type is more what you are after.

                  I still dont know what you are after actually (i dont want to go through your whole project to figure out, sorry). Is it just scoped shortcut handling?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • iwoitheI Offline
                    iwoitheI Offline
                    iwoithe
                    wrote on last edited by
                    #9

                    i dont want to go through your whole project to figure out

                    (Sorry, I should have said the following earlier) The only files that are important for the action controller are in main.cpp and in the actions directory.

                    raven-worxR 1 Reply Last reply
                    0
                    • iwoitheI iwoithe

                      i dont want to go through your whole project to figure out

                      (Sorry, I should have said the following earlier) The only files that are important for the action controller are in main.cpp and in the actions directory.

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @iwoithe
                      your QMLAction.qml can actually just be:

                      import QtQuick 2.15
                      import QtQuick.Controls 2.15
                      
                      Action {
                              id: action
                      }
                      

                      And when you create a button:

                      Button {
                             anchors.centerIn: parent
                             //text: qsTr("Print Hello")   //might also be taken from action
                             action: ActionController.getActionFromName("hello")
                      }
                      

                      Then from C++ you can do on the action instance:

                      Call method:

                      QMetaObject::invokeMethod(actionObj, "trigger", Qt::AutoConnection); // call method
                      or
                      QMetaObject::invokeMethod(actionObj, "trigger", Qt::AutoConnection, Q_ARG(QObject*,this));
                      

                      Read/Write property:

                      QVariant val = QQmlProperty ::read(actionObj, "text"):
                      QQmlProperty ::write(actionObj, "enabled", QVariant::fromValue<bool>(false)):
                      

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      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