Automatic connection of QML signal and C++ slot?
-
Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
Thanks! -
@GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?
@Tom-asso said in Automatic connection of QML signal and C++ slot?:
But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?
It’s better to call a C++ function from QML and do the plumbing in C++, I wouldn’t know of a use case where such connections are the best possible solution.
-
Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
Thanks!@Tom-asso
Yes in the sense that every binding is an automatic signal/slot connection.
No in the sense, that functional slots have to be explicitly connected. The syntax is quite simple, so I wouldn’t even call that a downside. -
@Tom-asso
Yes in the sense that every binding is an automatic signal/slot connection.
No in the sense, that functional slots have to be explicitly connected. The syntax is quite simple, so I wouldn’t even call that a downside.@Axel-Spoerl Design question for you:
If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?- declare/define 10 slot functions
- make 10 calls to connect(), once for each slot declared in step 1
That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
Do qtcreator or qtdesignstudio offer a more 'automatic' way?
Thanks! -
@Axel-Spoerl Design question for you:
If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?- declare/define 10 slot functions
- make 10 calls to connect(), once for each slot declared in step 1
That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
Do qtcreator or qtdesignstudio offer a more 'automatic' way?
Thanks! -
@Axel-Spoerl Design question for you:
If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?- declare/define 10 slot functions
- make 10 calls to connect(), once for each slot declared in step 1
That's the gist of it, right? Those 10 connect() calls just seem like a lot of application code to a lazy sob like me. ;-) Or is there an easier way to do it?
Do qtcreator or qtdesignstudio offer a more 'automatic' way?
Thanks!@Tom-asso said in Automatic connection of QML signal and C++ slot?:
If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?
- declare/define 10 slot functions
- make 10 calls to connect(), once for each slot declared in step 1
That's the gist of it, right?
Yes.
Those 10 connect() calls just seem like a lot
yy9pis the sequence in vim, doing 80% of this job for you. The rest is usually about editing a few letters :-)You can of course use autoconnect - totally fine, it has been designed for the comfort you are seeking.
The caveat is, that a mismatch of a single character will leave signal/slot unconnected without notice.
Depending on an exact string match in an XML file and C++ code, comes particularly unhandy when searching for a bug. I have seen furious forum posts and bugreports aboutQObject::connect()allegedly not working properly. I remember a project from a few years ago, where a company's quality department has done a huge effort inrenaming_all_underline_stuff intoCamelStyle, autoconnected slots included. Compiled like a charm, and set the UI on fire. -
Automatic connection of QtWidgets and C++ application code saves lots of development time. Is there a similar automatic connection feature for QML/QtQuick signals and application code, or do I have write my own calls to QObject::connect() for every QML Item that my C++ application code is concerned with? If automatic connection is not supported for this, why not?
Thanks! -
@Tom-asso don't use that feature if you can help yourself!
These days there's Ai/copilot to do repetitive boring and simple tasks. Like creating slots for signals or unit tests for functions.
Rather use that.
@J-Hilk said in Automatic connection of QML signal and C++ slot?:
These days there's Ai/copilot to do repetitive boring and simple tasks.
Nothing will ever equate to the efficiency and clarity of @Axel-Spoerl's
yy9pin vim ;-) -
@J-Hilk said in Automatic connection of QML signal and C++ slot?:
These days there's Ai/copilot to do repetitive boring and simple tasks.
Nothing will ever equate to the efficiency and clarity of @Axel-Spoerl's
yy9pin vim ;-) -
@JonB I actually have no idea what it is :P first time I heard about it.
But just wait until Ai is part of your OS and always listening, like Alexa....
Not a disturbing thought at all.
-
@J-Hilk If you don't know what
yy9pdoes in vi your life is lacking. Surely it's clear what it does anyway... ;) -
@J-Hilk If you don't know what
yy9pdoes in vi your life is lacking. Surely it's clear what it does anyway... ;)I'm sorry but do not connect to QML signals in c++, even though it's simple with some editor magic (Qt Creator ctrl-d is much easier to grok than vim bindings btw). You will still be left with verbose and subpar code.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s -
@Tom-asso said in Automatic connection of QML signal and C++ slot?:
If my C++ app must respond to multiple menu actions, e.g. 10 actions, then my C++ code must do the following, right?
- declare/define 10 slot functions
- make 10 calls to connect(), once for each slot declared in step 1
That's the gist of it, right?
Yes.
Those 10 connect() calls just seem like a lot
yy9pis the sequence in vim, doing 80% of this job for you. The rest is usually about editing a few letters :-)You can of course use autoconnect - totally fine, it has been designed for the comfort you are seeking.
The caveat is, that a mismatch of a single character will leave signal/slot unconnected without notice.
Depending on an exact string match in an XML file and C++ code, comes particularly unhandy when searching for a bug. I have seen furious forum posts and bugreports aboutQObject::connect()allegedly not working properly. I remember a project from a few years ago, where a company's quality department has done a huge effort inrenaming_all_underline_stuff intoCamelStyle, autoconnected slots included. Compiled like a charm, and set the UI on fire.@Axel-Spoerl autoconnect is only for Qt-Widgets, not enabled for QtQuick/QML, correct?
-
I'm sorry but do not connect to QML signals in c++, even though it's simple with some editor magic (Qt Creator ctrl-d is much easier to grok than vim bindings btw). You will still be left with verbose and subpar code.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s@GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?
-
@GrecKo - Thanks for those references! I think I generally agree with you now (after I wrote my entire freaking C++ app using signals/slots!). But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?
@Tom-asso said in Automatic connection of QML signal and C++ slot?:
But are there cases where QML signal to C++ slot makes sense? Maybe in the case of one QML emitter and multiple C++ 'listeners'?
It’s better to call a C++ function from QML and do the plumbing in C++, I wouldn’t know of a use case where such connections are the best possible solution.
-
@JonB so I asked chatgpt :P so I now know!
and I also generally try not to use a cmd line text editor if I can help it ;)
@J-Hilk said in Automatic connection of QML signal and C++ slot?:
I also generally try not to use a cmd line text editor if I can help it ;)
There also graphical versions of vi. And Qt Creator supports FakeVim which you can quickly toggle.
-
@J-Hilk said in Automatic connection of QML signal and C++ slot?:
I also generally try not to use a cmd line text editor if I can help it ;)
There also graphical versions of vi. And Qt Creator supports FakeVim which you can quickly toggle.
@SimonSchroeder I think @J-Hilk's interest in using vi/vim in any shape or form is absolute 0! We were jesting back & forth.
-
T Tom asso has marked this topic as solved on