View Full Version : How to automatically create boilerplate slot code from Ui form object?? Possible??

17th April 2018, 01:05

Was wondering if I have a qSpinBox on a UI Form like QWidget or QMainwindow the general signal that is sent when a value is changed is: on_mySpinbox_valueChanged(int) but was wondering how can I get QT Designer or Creator to automatically generate the Slot code for me??

Is this even possible? Just like when you <Alt>-<Enter> over a definition to generate the boilerplate code in the cpp file...


17th April 2018, 14:07
Figured it out - Right click go to slot..

17th April 2018, 16:25
I think you will find as you get more experience with Qt that using Qt Designer to create and connect your slots for you leads to a lot of head-banging. When you use Qt Designer like this, not only does it create the slot code at design time, it also connects the signals and slots at run time.

So now you have code that you are in control of (the connect() calls you make yourself) and code that does magic behind the scenes (the connect() calls made via setupUi() from the boilerplate code).

I don't know how many times I have sat with the debugger and puzzled about "How in the heck is this slot being called? I haven't called connect() anywhere." Eventually I start looking in the UI file and find there is an implicit signal / slot connection I had forgotten all about.

So now I don't allow Qt Designer to do any slot editing for me - all of it is done through explicit connect() calls in code -I- write. Yes, I have to type a few more lines, but I can see exactly what is happening with the UI and change it in one place.

17th April 2018, 18:11
So your saying its more efficient to hard code the connect signal and slot connections?

17th April 2018, 22:45
So your saying its more efficient to hard code the connect signal and slot connections?

Not necessarily. In one case you do a bunch of mouse clicks in Qt Designer, in the other you write a line of code.

The point is that by using Qt Designer to create and connect your signals and slots you can't open your cpp file and see everything that is going on in the UI. Instead, if you've made connections using Qt Designer, you need to look there in the .ui file for those, and if you've also made other connect() calls in your own code (to non-GUI QObject-based classes, for example), you need to look in the .cpp file also.

My preference is to make all connect() calls manually in the cpp file. I have everything in front of me where I can see it in one place and don't have to remember that some other connect calls occurred in a different place.