Qt的全局变量,如何?

时间:2021-08-28 23:23:02

I'm using Qt and in the main method I need to declare an object that I need to use in all my other files. How can I access that object in the other files? (I need to make it global..)

我正在使用Qt,在主方法中,我需要声明一个我需要在所有其他文件中使用的对象。如何访问其他文件中的对象?(我需要让它全球化。)

I'm use to iPhone development and there we have the appDelegate that you can use all over the application to reach objects you've declared in applicationDidFinishLaunching method. How can I do the same in Qt?

我在iPhone开发中使用了appDelegate,你可以在应用程序中使用它来达到你在applicationDidFinishLaunching方法中声明的对象。如何在Qt中做同样的事情?

3 个解决方案

#1


9  

global_objects.hpp

global_objects.hpp

extern int myGlobalInt;

global_objects.cpp

global_objects.cpp

#include "global_objects.hpp"

namespace
{
    int myGlobalInt;
}

And then #include "global_objects.hpp" in every place you need myGlobalInt.

然后#包括“global_objects。在任何地方都需要myGlobalInt。

You should read C++ singleton vs. global static object and Initializing qt resources embedded in static library as well.

您应该阅读c++单例对象和全局静态对象,并初始化嵌入在静态库中的qt资源。

#2


3  

In Qt there is the singleton QApplication, with its static method QApplication::instance() which gives you the one and only QApplication object back. It has many other static member functions (in an earlier age they were called "globals"), for the mainwindow, the active window etc.

在Qt中有一个单例QApplication,它的静态方法QApplication::instance()返回唯一的QApplication对象。它有许多其他的静态成员函数(在较早的时候被称为“globals”),用于主窗口、活动窗口等。

http://doc.trolltech.com/4.5/qapplication.html

http://doc.trolltech.com/4.5/qapplication.html

You can sublass it if you want to add your own statics.

如果你想添加你自己的静力学,你可以将它细分。

#3


1  

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.

一般来说,在面向对象编程中不应该使用全局变量。在您的示例中,可以通过为主类中的变量提供静态访问函数来解决这个问题。不过,您应该知道,这与OOP有些相反。

class MainClass
{
    public:
        static int mySharedValue(void) { return m_mySharedValue; }
        static void setMySharedValue(int value) { m_mySharedValue = value; }
    private:
        static 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.

此外,您可以从QApplication派生主应用程序并在其中添加访问函数,通过Qt提供的qApp指针访问主对象。

#1


9  

global_objects.hpp

global_objects.hpp

extern int myGlobalInt;

global_objects.cpp

global_objects.cpp

#include "global_objects.hpp"

namespace
{
    int myGlobalInt;
}

And then #include "global_objects.hpp" in every place you need myGlobalInt.

然后#包括“global_objects。在任何地方都需要myGlobalInt。

You should read C++ singleton vs. global static object and Initializing qt resources embedded in static library as well.

您应该阅读c++单例对象和全局静态对象,并初始化嵌入在静态库中的qt资源。

#2


3  

In Qt there is the singleton QApplication, with its static method QApplication::instance() which gives you the one and only QApplication object back. It has many other static member functions (in an earlier age they were called "globals"), for the mainwindow, the active window etc.

在Qt中有一个单例QApplication,它的静态方法QApplication::instance()返回唯一的QApplication对象。它有许多其他的静态成员函数(在较早的时候被称为“globals”),用于主窗口、活动窗口等。

http://doc.trolltech.com/4.5/qapplication.html

http://doc.trolltech.com/4.5/qapplication.html

You can sublass it if you want to add your own statics.

如果你想添加你自己的静力学,你可以将它细分。

#3


1  

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.

一般来说,在面向对象编程中不应该使用全局变量。在您的示例中,可以通过为主类中的变量提供静态访问函数来解决这个问题。不过,您应该知道,这与OOP有些相反。

class MainClass
{
    public:
        static int mySharedValue(void) { return m_mySharedValue; }
        static void setMySharedValue(int value) { m_mySharedValue = value; }
    private:
        static 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.

此外,您可以从QApplication派生主应用程序并在其中添加访问函数,通过Qt提供的qApp指针访问主对象。