Is there way to use QT Designer UI forms so you get direct access to the form widgets in your program?
-
I'm having trouble finding a way to use multiple UI forms in one program so that I get direct access to the widgets in the forms. Direct access would make it much easier to implement my backend and connect it the frontend form files. As it is now I have to use public modification methods to change widgets in the forms and also intermediate signals and slots to communicate between the form's widgets and the code in my backend class. It would be much easier to just change the widgets directly and connect them directly to slots in my backend class. Can something like this even be done? Thanks
-
Hi,
Yes it might be done but it's the first step to tight coupling and maintenance nightmare (you don't want either for them).
How are you implementing your backend ?
-
-
That's why I asked about the backend implementation ;)
Your widgets should be responsible for connecting to the backend so one way to do that is to add a
setupBackend
method to them and in there do all the connection needed. Doing so you have access directly to said widget internal details thus if you modify it, you only have to change code within that widget. This keeps your code clean and easy to maintain. -
You're talking about my form top level widgets right? So I just add the method to the form class? How do I connect signals and slots between the form and the backend then? Also curious about your mention of the possibility of direct access in the first reply - can you elaborate a little?
Thanks again -
I mean any widgets you build that would need a connection to your backend.
You do the connections in that method i.e.:
void MyCoolWidget::setupBackend(MyCoolBackend *backend) { connect(backend, &MyCoolBackend::stringBasedSignal, ui->statusLabel, &QLabel::setText); // etc. }
That way all the code stays in MyCoolWidget and if you change that class e.g. you replace the QLabel by a QLineEdit, you only have to modify setupBackend. No need to hunt every line where you could have done a connection between MyCoolWidget and MyCoolBackend.
You can give access to the ui variable from your widgets. Again, I very strongly advice to not do such a thing.
-
What if I have to go the other way around? - click a button on a widget and do some backend stuff. Also as you probably figured out I'm a newbie to all this stuff. I have a background in data structures and basic programming and inheritance etc... - I'm basically an intermediate level programmer. Can you recommend any books or tutorials to help me out with this scenario of backend vs fronted and ui forms specifcally QT? Thanks :)
-
Same principle:
void MyCoolWidget::setupBackend(MyCoolBackend *backend) { connect(backend, &MyCoolBackend::stringBasedSignal, ui->statusLabel, &QLabel::setText); connect(ui->startButton, &QPushButton::clicked, backend, &MyCoolBackend::igniteToaster); // etc. }
This book might be of interest. Look for the parts about the Façade pattern.
-
@Crag_Hack
Hi
When i started with Qt, i like to browse around in this one
http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf
I find it goes well with Qt docs to learn about different classes and ways to structure the app. -
What kind of special cool looking buttons do you have in mind ?
-
For the first one I'd recommend going with QtQuick, that will be easier to build. The second can be accomplished with widgets with some styling but again, you'll likely be faster with QtQuick to do it.
-
I'd rather not use QT Quick unless it's necessary because I'm not familiar with it. Maybe I should bite the bullet and learn it... With widgets though do those books teach you how to do gradients / style sheets / icons and graphics etc so you can make great looking programs? Also if QT Quick is super easier and more effective can I integrate with my C++ backend?