Data variable scope
mchris357 last edited by
Hello again, hopefully with the last intro level question I'll have for a while (though I doubt it :)).
I'm really starting to get the hang of how QT makes GUI interfaces so much easier to deal with, but being more of a command prompt programmer the "jumping around" from visual object to object one is very new. (Particularly signals and slots, though they are fun).
I'm now to the point where I'm actually adding in some of my "functionality" classes (calculations and other boring stuff that ends up in all the pretty displays), and I'm curious how you folks like to deal with your data classes. Scope is always an issue dealing with these, and I'm not a big fan of global scope if it isn't necessary.
That said, because there are so many "individual" objects in QT that may interact with the "functionality" classes, it's difficult to select a way to make it accesible to all of those objects.
In my program, there are multiple ways to enter data (same form, different means of accessing it). You can select file->new which pops up the box and allows you to input the data. After you've entered it, it shows up in disabled text field boxes. The only way to edit it once it is there is to press an "Edit Parameters" button. When you do that, you have an already filled in form that you can edit.
Both of these objects (MainMenu and a subelement of the page) need to access this data, as well as any classes that will actually perform processing. This doesn't even consider file I/O etc...
My data is all accessible through a hierarchy of linked lists and arrays that are pointed to by a single "linked list" class object. In particular, this list has a head pointer object that can get you to everything. If I'm going to need to access this in many elements of the program, is it best declared as global or are there better ways to link it to everthing it needs to be linked to?
I know this is more of a general C++ GUI programming question, so thanks to anybody who takes the time to answer :).
Edit: moved to C++ Gurus as it's more a C++ design then a Qt related issue, Gerolf
giesbert last edited by
There are several ways to achieve this. If the data exists only once in the process,
you could use
- "singleton pattern":http://en.wikipedia.org/wiki/Singleton_pattern
- "monostate pattern":http://c2.com/cgi/wiki?MonostatePattern ("comparison of these two":http://de.w3support.net/index.php?db=so&id=887317 ).
- global variables (which I personally don't like).
- Put the data to a custom QApplication derived class. access to the QApplication object is possible via the qApp macro of Qt.
- Add those tdata to your main window class and give the sub objects pointer to the main window (or use the QApplication::topLevelWidgets() to access it)
I personally like custom QApplication class if the data is application global.
andre last edited by
A variation on the custom QApplication option Gerolf mentions, is using the fact that QApplication is a QObject, and thus, supports dynamic properties. You can dynamically add data to QApplication without subclassing it. I am not claiming that this is the best solution, just mentioning it...
A downside from a design perspective is that it might be unclear. There is no clearly defined interface for your data, as you are using a generic interface on an already existing singleton. Don't start using QApplication to dump all your loose pieces of data into though, that would not result in a clear and understandable design.
ZapB last edited by
I would be tempted to use the singleton pattern for your top-level list. It is quick and easy to do and is easy to refactor if you decide to ever provide a non-gui version of your app.