Unsolved where should action execution happen?
-
i have a module like class, which is
QObject
.
the module creates a lot of actions.
i don't want to add as much slots in the module, instead i want to create a class which consists of only public slots, for each of the actions.
i want to know if this is a good pattern to use -
@user4592357
Slots would be best defined where the body of the code they execute is closest to the objects it needs to access. For example, if your slots spend a lot of their time accessing objects/widgets defined in yourMainWindow
class, it's all very well putting them elsewhere as you say but then you are likely to have parameters to them to access theMainWindow
, and possibly its private variables. Which is not good.That is a separate question from whether you want to, say, derive your own class from
QAction
to use. As usual with Qt signals/slots, the module which creates aQAction
andconnect()
s its signals to slots must simply have visibility of both the signals and the desired slot objects. -
@JonB
i have main window pointer and add the actions to main window from the module, so I can't place the slots in main window.neither i want to place them in module.
so i thought i would need to create a new class, actionExecutor or whatever -
@user4592357 said in where should action execution happen?:
i have main window pointer and add the actions to main window from the module, so I can't place the slots in main window.
Precisely! Which doesn't sound very good :) It sounds like you have a number of widgets with actions and slots to do stuff all of which belong in/access members in your main window class? In which case, trying to create some other module to offer the slots for the actions is likely to make it bad just as you are saying....
-
@JonB
why is it bad? if my module is very smart thing, i may give it only to people who pay for it :)so it makes sense to add actions in module.
so, should i create this new actionExecutor class and have public functions in it, and connect my actions to these functions?
-
@user4592357 Usually a class which emits signals should not know anything about classes which connects to its signals. I suggest you provide an interface in your model to get the actions, then your main window can get these actions and connect to them - your model does not have to care about classes connecting to it.