Where should I connect signals with slots
-
Currently, I have about 10 classes in my project. I had included all other class headers to my mainwidget.h. Created objects and instantiated them in header. And in my mainwidget.cpp i am using
connect()keywords for all signals and slots in project. Normally how should i do this?For example now I have
ClassX, it emitsstartSignalandClassY'sstartSlotwill be responsible with that signal, and after doing its job it need to disconnect the signal. Maybe I can pass ClassX object to ClassY when creating objects in mainwidget, and connect them in ClassY. I am not sure that should be done in this way.Shortly, I am demanding for advices, how to/where to connect signals and slots properly.
-
Currently, I have about 10 classes in my project. I had included all other class headers to my mainwidget.h. Created objects and instantiated them in header. And in my mainwidget.cpp i am using
connect()keywords for all signals and slots in project. Normally how should i do this?For example now I have
ClassX, it emitsstartSignalandClassY'sstartSlotwill be responsible with that signal, and after doing its job it need to disconnect the signal. Maybe I can pass ClassX object to ClassY when creating objects in mainwidget, and connect them in ClassY. I am not sure that should be done in this way.Shortly, I am demanding for advices, how to/where to connect signals and slots properly.
@masa4 said in Where should I connect signals with slots:
Created objects and instantiated them in header
In header?! This is usually done in cpp, not in header files.
If ClassY needs to disconnect from the signale then yes you can pass pointer to ClassX to ClassY, so ClassY can connect and later disconnect from the ClassX signal.
If there is no need to disconnect from signal then you can simply call connect where you're creating ClassX and ClassY instances. -
@masa4 said in Where should I connect signals with slots:
Created objects and instantiated them in header
In header?! This is usually done in cpp, not in header files.
If ClassY needs to disconnect from the signale then yes you can pass pointer to ClassX to ClassY, so ClassY can connect and later disconnect from the ClassX signal.
If there is no need to disconnect from signal then you can simply call connect where you're creating ClassX and ClassY instances. -
@jsulm Thanks for answer. As an additional question, is it ok too?
mainwidget.h:ClassX *x; ClassY *y;mainwidget.cpp(constructor):
x = new ClassX(y); y = new ClassY(x);And yes I need to move instantiation to .cpp.
@masa4 Yes, this is good.
If you're using pointers you do not even need to include ClassX/ClassY header files in mainwindow.h. Instead use forward declaration and include the header files in mainwindow.cpp:// In mainwindow.h class ClassX; // Forward declaration for ClassX class ClassY; // Forward declaration for ClassY ClassX *x; ClassY *y; // In mainwindow.cc #include "classx_header_file.h" #include "classy_header_file.h" x = new ClassX(y); y = new ClassY(x);This technique makes header files smaller and helps to reduce compile time.
-
@jsulm Thanks for answer. As an additional question, is it ok too?
mainwidget.h:ClassX *x; ClassY *y;mainwidget.cpp(constructor):
x = new ClassX(y); y = new ClassY(x);And yes I need to move instantiation to .cpp.
-
@masa4 said in Where should I connect signals with slots:
x = new ClassX(y); y = new ClassY(x);In the first line the value of
yis undefined -
@masa4 said in Where should I connect signals with slots:
x = new ClassX(y); y = new ClassY(x);In the first line the value of
yis undefined -
@jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.
-
@jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.
@masa4
It is unlikely each object should want a reference to the other object, that tends to lead to mutual recursion.Slots should be connected to signals from somewhere which can "see" both the signal and the slot. This might be in the slot class, or it might be in another class which includes both signal & slot classes. What you should not do is connect to an external slot class from the signal class --- slots may know about signals, but signals should never know about slots.
-
@jsulm I want to emit a signal from X class for a slot in Y class. Y will disconnect after its process done. And I want the y object in X class because I will send another signal if Y is visible. In this situation i thought i need each object in each class.
@masa4 said in Where should I connect signals with slots:
And I want the y object in X class because I will send another signal if Y is visible
Who will emit this other signal? X or Y?
In general you should avoid such circular dependencies - this is bad design. -
@masa4 said in Where should I connect signals with slots:
And I want the y object in X class because I will send another signal if Y is visible
Who will emit this other signal? X or Y?
In general you should avoid such circular dependencies - this is bad design.