Solved 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 useQAction
but with no success. What is the best way to implement this?Thanks
-
-
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++).
-
@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++?
-
@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 -
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 thatQAction
is not a registered QML type. Should I be usingQAction
at all? How should I adjust the code so that theAction
QML type can be used instead ofQAction
?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)
-
@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?
-
-
@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)):