PDA

View Full Version : signal slot to mainwindow



giugio
9th November 2012, 18:58
hello.
I'm read about the behaviour of signal/slot in qt.
My problem is that i have created a custom widget and the emission of the signal(emit pressed()) is in a custom delegate of my project.
How i can "take out" the signal for invoke the "pressedButton()" slot that is in the main window?
i must send the main window pointer(m_pMainWindow) and transport it to the delegate?

where i can do:


connect(this, SIGNAL(pressed()),
m_pMainWindow, SLOT(prssedButton())));


is correct?
thanks.

amleto
9th November 2012, 19:25
that is bad design. Normally you should do it the other way around:




connect(my_custom_widget, SIGNAL(pressed()),
this, SLOT(pressedButton())));



This is so that clients of the signal do not have to be known to the emitter. This maintains dependency injection principles.

giugio
9th November 2012, 19:42
thanks, another thing :
I have this code in the main function , but the "connect" function works only if the class inherits from QOBject(is correct?) and the main function is not a class function.
then I have a openglwidget that inherits from qobject and the opengl code that don't inherits from qobject how i can manage all the signals and slots?
I must create a central class(that inherits from QObject) that works as a bridge from qt to opengl and other possible subsystems?
thanks.

amleto
9th November 2012, 20:05
you can just use


QObject::connect(qobjectPtr1, SIGNAL(some_signal()), qobjectPtr2, SLOT(some_slot()));