PDA

View Full Version : Sharing data between objects



Mr_Grieves
26th January 2009, 14:27
This is probably a simple c++ question, but it's driving me up a wall.

Let say I have two variables:


bool running;
QList<double> * data;
The first simply tracks if a timer is running, and the second is a pointer to a reasonably large list of numbers. I want to be able to access these vars from every QFrame, QwtPlot, QWidget, etc in my program. As of now, I'm passing each through the constructor of each QObject. Previously I've set up nasty networks of signals and slots that bounce around between the objects to find one where the variable is in scope. There has got to be a better way.

Is this the sort of thing that is allowable for declaring globally in main.cpp? There are so many warnings about that being bad...What is the Qt way of sharing these variables throughout my program?

wysota
26th January 2009, 14:54
You can declare a global variable or implement a singleton class for holding it.

kandalf
27th January 2009, 00:13
I would recommend a singleton class.
I think global variables are a bad practice, but nothing more. I don't think they're as nasty as their detractors can say, but they're, certainly, not the neater solution either. I, personally, try to not to use them.

Cheers