PDA

View Full Version : Bad design decision in Designer



gkhrapunovich
14th February 2011, 17:16
I am new to QT and just want to bring your attention to one minor feature of designer which caused a lot of confusion and lost time for me. I have a button btnConnect on the form and wanted to create a custom slot for clicked() signal. Designer added the following code to the generated .h file:
...
QObject::connect(btnConnect, SIGNAL(clicked()), MyProjectQtClass, SLOT(btnConnect_Clicked()));
...

My next step should be to add the slot for appropriate class. When I searched for "MyProjectQtClass", I found the line on the bottom of the same generated .h file:

namespace Ui {
class MyProjectQtClass: public Ui_MyProjectQtClass {};

I could not add any functions to that class because its definition was a part of compiler-generated header! It took me a while before I realized that this is pure coincidence and these are two different classes. "MyProjectQtClass" in the connect() call is in fact a parameter to the setupUi() function and "MyProjectQtClass" declared at the end of generated header is unrelated to it.

I think it was a bad decision for Designer programmers to use the same spelling for two unrelated items. It can be very easily fixed in the future.

high_flyer
14th February 2011, 18:01
The problem is not with designer, but with you misunderstanding how things work.

I could not add any functions to that class because its definition was a part of compiler-generated header!
You are not supposed to!
At least, not in the generated header - for the very reason, that it is auto generated, and should you change something in the ui file, any chanes you made in the generated header will be lost.
The correct way to use the generated class is through inheritance.

It took me a while before I realized that this is pure coincidence and these are two different classes.
There are no coincidences here - and these are not two different classes - but pointers to objects!
The name you put in the "objectName" field is the name the VARIABLE of the class will have, and that is also what connect is using.

Be sure to read and understand this here (it might save you some confusion and time in the future ;) )
http://doc.trolltech.com/4.7/designer-manual.html
and specifically this:
http://doc.trolltech.com/4.7/designer-connection-mode.html
and this, for using your designer forms in your application (adding slots etc):
http://doc.trolltech.com/4.7/designer-using-a-ui-file.html