Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Where to place a connect statement?

Tags:

c++

qt

I have a widget (mainWidget) and another widget (childWidget), that's a child of it.

I want to:

connect(childWidget, SIGNAL(somethingHappened(...)), mainWidget, SLOT(handleIt(...));

My question is: does one place the connect statement in mainWidget or childWidget?

  • If I create the childWidget in the mainWidget's constructor and place the connect statement on the next line, it works. But, let's say the childWidget, upon being created, does something and then signals to
    the mainWidget success. You could have a situation where the connect statement only comes after a function (of childWidget) that emits the signal.

  • If I place the connect statements in the childWidget's constructor,
    the problem is that it doesn't know anything about the parent's
    slots. If I make childWidget inherit mainWidget, it does know about
    the slots - but this feels like a bad solution to me. Couldn't get it to work anyway. There is probably a proper way to do this - I'm still looking.

I'm quite new to Qt programming. In advance: thank you for any help.

like image 345
AJO_ Avatar asked Dec 14 '25 17:12

AJO_


1 Answers

You should design your code well to avoid creeping of mysterious bugs. It would be a better idea to write the connect in the main widget and ideally there should not be an emit in the constructor of the child widget. Possibly you could move out the emit code to another block and do the call after construction is complete. Sub-classing 'childwidget' from 'mainWidget' only to get access to its slot looks like an inflexible design. Design should be such that if any class knows the signal your class emits, the other class should be able to flexibly connect to it.

like image 194
mots_g Avatar answered Dec 17 '25 11:12

mots_g



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!