In general, you shouldn't use global variables in object oriented programming. In your case, you can solve the problem by providing static access functions to the variable in your main class. You should be aware, though, that this is somewhat contrary to OOP.
class MainClass
{
public:
static int mySharedValue(void) { return m_mySharedValue; }
static void setMySharedValue(int value) { m_mySharedValue = value; }
private:
int m_mySharedValue;
}
Foo::myOtherClassFunction(void)
{
// do something
int bar = MainClass::mySharedValue();
// do some more
}
Furthermore, you can derive your main application from QApplication and add the access functions there, accessing the main object through the qApp
pointer provided by Qt.
Additionally to that you can always use a global variable the same way you can do in C, though I wouldn't recommend that.