I have a Qt-based application that uses a third party OpenGL-based library (OpenSceneGraph) to display my scene (via QOpenGLWidget
). Basically, I use Qt for everything GUI, and OpenSceneGraph (OSG) for my scene content and event handling (the events are passed to OSG from QOpenGLWidget
).
The application is small scale CAD-like and involves lots of interaction of user (via both GUI and OSG events) with the scene. For the interaction organization between Qt and OSG (and back - OSG and Qt), most of the time I use signals and slots. Some of my OSG-based classes inherit QObject
so that to have the signal and slot functionality. So, in general it looks like this:
class OpenGLBasedScene : public QObject, public osg::Node{};
Recently I implemented a singleton design into my QMainWidget
, since it became somewhat challenging to use signal and slots for certain OSG classes of my scene graph.
And now I question: is singleton design preferred over the signals and slots in the mixed environment, especially when we deal with non-Qt-based classes? For example: user is editing the scene by means of OSG event handling, and those scene edits must be reflected on certain GUI elements. When using singleton design, instead of inheriting from QObject
and signal emittance, I can simply use a call:
// function where user is interacting with scene graph content by means of third-party event handling
MainWindow::instance().getThatUI()->doThisThingToIt();
For the opposite direction, i.e., user changes the scene by the means of GUI elements, using of signals and slots seems natural and there is no question about the preferred design pattern. So, my question is more related to the direction third party class to GUI element (OSG to Qt).
From my observation on the later (OSG to Qt):
- Signals and slots seem to make reading of the code more difficult. While I try to keep the naming conventions as clear as possible, I often have to use
Ctrl+Shift+F
to find to what widgets a signal is connected to. - At the same time, using singleton pattern makes code not only more readable, but also helps to simplify (might not need to pass certain parameters since we can obtain them by using call to
instance()
).
However, are the above reasons enough to use singleton pattern over signal-slots in the given settings? I am not familiar with the singleton design pattern usage, and do not know what kind of disadvantages it may give. Would be nice if someone could explain in terms of best practices, what should be preferred and for what reason. I ask this question because I'm considering whether to replace all the signals/slots from OSG-based classes on using the singleton or not.
To summarize the question, given two ways of keeping the application elements (Qt-based and OSG-based) synced with each other - by means of signals-slots and singleton calls; what is the preferred design pattern in a case when we want the introduced changes to OSG class be reflected on Qt GUI element?
Aucun commentaire:
Enregistrer un commentaire