C++ Qt中的全局变量,怎么做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1471764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variable in Qt, how to?
提问by Martin
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,并且在 main 方法中,我需要声明一个我需要在所有其他文件中使用的对象。如何访问其他文件中的该对象?(我需要让它成为全球性的..)
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 中做同样的事情?
回答by Piotr Dobrogost
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
.
然后#include "global_objects.hpp"
在您需要的每个地方myGlobalInt
。
You should read C++ singleton vs. global static objectand Initializing qt resources embedded in static libraryas well.
回答by Gunther Piez
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 对象。它还有许多其他静态成员函数(在早期它们被称为“全局函数”),用于主窗口、活动窗口等。
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.
如果您想添加自己的静力学,您可以将其 sublass。
回答by bluebrother
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 派生您的主应用程序并在那里添加访问函数,qApp
通过 Qt 提供的指针访问主对象。除此之外,您始终可以像在 C 中一样使用全局变量,但我不建议这样做。