setupUi() is not called in the auto generated code, but in your code... or I misunderstood you...
setupUi() is not called in the auto generated code, but in your code... or I misunderstood you...
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Correct, from the constructor of the derived class QMainWindow.
Qt Code:
{ ui.setupUi(this); // UI is only partially built from Qt Designer, connectSlotsByName // is called in setupUi. BuildRestOfUI(this); // Can't call connectSlotByName a second time, otherwise each signal // slot pairing from the previous call will add another signal slot pairing // resulting in those slots getting 2 signals (called twice). }To copy to clipboard, switch view to plain text modeQt Code:
//** Form generated from reading UI file 'MainWindow.ui' { if (MainWindowClass->objectName().isEmpty()) MainWindowClass->resize(766, 542); // >>snip<< retranslateUi(MainWindowClass); } // setupUiTo copy to clipboard, switch view to plain text mode
Oh now I see what you mean (I think) - you DO want to call setupUi() when you do, but you don't want it to call the signal/slot auto connection at that time, is that correct?
Do you have to use auto-connect?
If you don't - you can delete the connections done by designer, this way setupUi() will not call the connections, and you can make the connections at any time later that suits you.
If you have to use the auto connections, then maybe you need a different approach.
Have a look at QAbstractFormBuilder, it might give you the flexibility you want, without giving up on the ui files.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
pkohut (29th December 2010)
I would suggest to not rely on connectSlotsByName() at all and instead make all the connections manually. auto-connecting slots is a dirty hack that shouldn't be used as it requires the slots to have weird names and the mechanism often backfires if you name slots in this manner by accident and you end up with a double connection or no connection at all (if you rename some object later on).
pkohut (29th December 2010)
Correct.
Prior to the initial posting I did that, however the results were unexpected (to me). Here's the code. Running it with QMetaObject::connectSlotsByNames commented out in setupUi resulted in found connections (wasn't expecting any). Will do some research as to what connections those are and if safe to disconnect and will they reconnect with a later call to connectSlotsByNames. Post findings later.
Qt Code:
{ ui.setupUi(this); bool bFoundConnections = false; bFoundConnections |= obj->disconnect(); } if(bFoundConnections) { qDebug() << "It appears QMetaObject::connectSlotsByName was called" << "in ui.setupUi, make sure that line of code is commented out."; } BuildResultOfUi(this); }To copy to clipboard, switch view to plain text mode
Don't need auto connections, just trying to come out of the stone age of hand coding every thing.
Thanks very much for your help.
Added after 5 minutes:
That's a good point. There is one slot/signal pair so far that just won't auto connect (spent about 30 minutes on it so far), I'm sure it's syntax, however it looks correct.
Last edited by pkohut; 28th December 2010 at 20:59.
Like wysota, I don't like auto connections, and never used them.Don't need auto connections, just trying to come out of the stone age of hand coding every thing.
Nothing stone aged about connecting your self (IMHO) - its a matter of control over your code.
If you don't need auto connections, and even better, if you don't need the connections done at ui design time (in designer) just get rid of them (any connections in the ui), make them manual - it will save you all the this headache!
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
As a side note, there is a quick way to disconnect all connections made to an object so a potential solution to your problem is to do just that (by iterating over children and calling disconnect() variant that breaks all the connections between objects) and then to call connectSlotsByName() when you need it. But I really advise you to not use it and instead make all the connections conciously.
I'm taking the advise of you and high_flyer and forgoing auto connections. However, was still wondering if breaking the connections then calling connectSlotsByName would be sufficient. From the code below I'd say the answer is no.
Qt Code:
{ // connectSlotsByName called in setupUi ui.setupUi(this); // Get count of disconnected slots and signals int nConnectionsA = 0; nConnectionsA += pObj->disconnect(); // reconnect slots by name // Get count of disconnected slots and signals int nConnectionsB = 0; nConnectionsB += pObj->disconnect(); // Test run here nConnectionsA == 9 and // nConnectionsB == 0 Q_ASSERT(nConnectionsA == nConnectionsB); }To copy to clipboard, switch view to plain text mode
Your code is not doing what you wanted to do:
Qt Code:
int nConnectionsA = 0; if( pObj->disconnect()) nConnectionsA ++; } // reconnect slots by name // Get count of disconnected slots and signals int nConnectionsB = 0; if( pObj->disconnect()) nConnectionsB ++; }To copy to clipboard, switch view to plain text mode
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Hmm, what did I miss? QObject::disconnect() returns type bool. Through implicit conversion from type bool to type int , true is 1 and false is 0.
Just to make sure it isn't a compiler thing I tested this is VC and gcc.
Output is: true 1Qt Code:
bool bVal = (bool) 200; int nVal = bVal; qDebug() << bVal << nVal;To copy to clipboard, switch view to plain text mode
You are right.
But I think such tings are potentially dangerous.
Since true doesn't necessarily has to mean '1' (all though I just checked, and the c++ indeed specifies true as '1').
But some environments or applications redefine bool,
But in this case you are right.
what does this->children().size() return in both cases?
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
But what is the point of doing so? Could you tell me what the following slot does?
Qt Code:
void Cls::on_btn4_clicked() { /* ??? */ }To copy to clipboard, switch view to plain text mode
Isn't it better to call it say... "chooseBrushColor()" instead?
Bookmarks