在Qt中,可以通过以下方式来定义全局数组:
- 在一个头文件中定义数组,并在对应的源文件中进行初始化:
// global.h #ifndef GLOBAL_H #define GLOBAL_H extern int globalArray[10]; #endif // global.cpp #include "global.h" int globalArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- 在main函数中定义全局数组:
// main.cpp
#include
#include "mainwindow.h"
int globalArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
无论哪种方式,都可以在整个程序中访问和修改全局数组globalArray
。