1. use extern
-
// globalvar.h
-
-
#ifndef MYGLOBALS
-
#define MYGLOBALS
-
-
extern int variable1;
-
extern int variable2;
-
-
#endif
-
-
...
-
-
// globalvar.cpp implementation
-
-
int variable1 = 0;
-
int variable = 0;
2.use global property in main.cpp
-
..
-
...
-
main (... )
-
{
-
...
-
a. setProperty ( "my_global_string" , my_global_string ) ;
-
...
-
MyClass my_class ;
-
...
-
-
return a. exec ( ) ;
-
-
}
-
MyClass :: MyClass ( )
-
{
-
...
-
qDebug ( ) << qApp -> property ( "my_global_string" ) ;
-
...
-
}
本文介绍了两种定义和使用全局变量的方法:一是通过头文件声明并在单独的cpp文件中定义;二是利用Qt框架特性,在主程序中设置全局属性,并在类内部通过qApp获取。这两种方式都便于管理跨模块的数据。
1453

被折叠的 条评论
为什么被折叠?



