C++应该是不提倡实用全局变量的,但是有时候做个小东西或者小项目,使用全局变量还是很方便的
新建一个头文件Data.h, 在其中声明全局变量
#ifndef DATA_H
#define DATA_H
extern QStringList names;
extern QStringListpasswords;
extern QStringListauthority;
extern QStringList admins;
extern QString name;
extern QString password;
#endif // DATA_H
再新建一个cpp文件,定义全局变量
#include <QString>
#include <QStringList>
QStringList names;
QStringList passwords;
QStringList authority;
QStringList admins;
QString name;
QString password;
使用全局变量的时候,只要包含头文件即可