C++ Qt 5.3 中的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23922319/
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 variables in Qt 5.3
提问by Pavlo Zvarych
In Visual Studio 2012 (C++) it is enough to declare variable at the beginning for it to have global scope and at the same time set the value for the variable. How to create global variable and initialize in Qt 5.3?
在 Visual Studio 2012 (C++) 中,只需在开头声明变量即可使其具有全局作用域,同时设置变量的值。如何在 Qt 5.3 中创建全局变量并初始化?
I tried to declare it in header file, but I have a problem: "only static const integral data members can be be initialized within a class".
我试图在头文件中声明它,但我有一个问题:“只能在类中初始化静态常量整数数据成员”。
Thanks in advance!
提前致谢!
回答by phyatt
Global Variables
全局变量
To create a "global" variable, you need to make it available to everyone and you need to make it declared once, and only once.
要创建“全局”变量,您需要使其对所有人可用,并且需要声明一次,并且仅声明一次。
globals.h
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include <qtglobal.h>
// ALL THE GLOBAL DECLARATIONS
// don't use #include <QString> here, instead do this:
QT_BEGIN_NAMESPACE
class QString;
QT_END_NAMESPACE
// that way you aren't compiling QString into every header file you put this in...
// aka faster build times.
#define MAGIC_NUM 42
extern qreal g_some_double; // Note the important use of extern!
extern QString g_some_string;
#endif // GLOBALS_H
globals.cpp
全局变量
#include "globals.h"
#include <QString>
// ALL THE GLOBAL DEFINITIONS
qreal g_some_double = 0.5;
QString g_some_string = "Hello Globals";
Now at the top of any file you want access to these dangerous global variables is:
现在,您想要访问这些危险的全局变量的任何文件的顶部是:
#include "globals.h"
// ...
// short example of usage
qDebug() << g_some_string << MAGIC_NUM;
g_some_double += 0.1;
In summary, globals.h
has all the prototypes for your global functions and variables, and then they are described in globals.cpp
.
总之,globals.h
拥有全局函数和变量的所有原型,然后在globals.cpp
.
public static member variables and methods
公共静态成员变量和方法
For these they are similar to the above example, but they are included in your class.
对于这些,它们类似于上面的示例,但它们包含在您的类中。
myclass.h
我的类.h
class MyClass
{
public:
static int s_count; // declaration
}
myclass.cpp
我的类.cpp
int MyClass::s_count = 0; // initial definition
Then from any part of your program you can put:
然后从程序的任何部分,您可以放置:
qDebug() << MyClass::s_count;
or
或者
MyClass::s_count++;// etc
DISCLAIMER:
免责声明:
In general globals and public static members are kind of dangerous/frowned upon, especially if you aren't sure what you are doing. All the OOP goodness of Objects and Methods and Private and Protected kind of go out the window, and readability goes down, too. And maintainability can get messy. See the more in depth SO answer below:
一般来说,全局变量和公共静态成员有点危险/不受欢迎,尤其是当您不确定自己在做什么时。对象和方法以及私有和受保护的所有 OOP 优点都消失了,可读性也下降了。并且可维护性会变得混乱。请参阅下面更深入的 SO 答案:
QSettings
Q设置
For some global settings, I've used QSettings
with great success.
对于一些全局设置,我已经QSettings
非常成功地使用了。
http://qt-project.org/doc/qt-5/QSettings.html#details
http://qt-project.org/doc/qt-5/QSettings.html#details
https://stackoverflow.com/a/17554182/999943
https://stackoverflow.com/a/17554182/999943
Hope that helps.
希望有帮助。