Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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


  • Moderators

    @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?



  • 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++).


  • Moderators

    @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)



  • 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++?


  • Moderators



  • 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)


  • Moderators

    @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?



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


  • Moderators

    @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)):
    

Log in to reply