QT 定义使用全局变量的方法

1、跟C、C++那样用extern

2、新建一个类,存放全局的变量(使用全局变量这里举例就是第二种方法)

定义
student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <QString>

/* 新建类 学生信息 */
class Student
{
public:
    Student();
    ~Student(){}

    QString id;             /* 学号 */
    const static QString idDefault;/* 默认学号 */
};

#endif // STUDENT_H


student.cpp

#include "student.h"
#include <QDebug>

const QString Student::idDefault = "1300810434";/* 学号默认值 */
Student::Student()
{
    id = Student::idDefault;/* 赋id默认值 */
    qDebug()<<"Student id"<< id ;
}

info.h

#ifndef IN_FO_H
#define IN_FO_H

#include "student.h"

/* 信息类包含学生信息 */
class Info
{
public:
    Info();
    static Student stu;/* 学生信息 */
    /* 其他信息 老师、医生*/
};

#endif // AC_PANEL_H

info.cpp

#include "info.h"
#include <QDebug>

Student Info:: stu;/* 全局变量、函数放到stu这个类中 */

Info::Info()
{
    Info::stu.id= "202000810434";/* 更改学生信息 */
    qDebug()<<"Info id"<< Info::stu.id;/* 打印更改后学生信息 */
}

使用

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "info.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label->setText(Info::stu.id);/* 两种信息显示方式label、qDebug */
    qDebug() << "MainWindow id"<< Info::stu.id ;
}


MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

#include "info.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Info info;/* 信息实例化 */
    MainWindow w;   
    w.show();

    return a.exec();
}
Qt中,多文件全局变量使用可以通过以下几种方式实现: 1. 定义全局变量,在需要使用的文件中使用extern关键字声明 定义一个全局变量,在一个文件中使用,然后在其他文件中使用extern关键字声明该变量即可使用: // global.h #ifndef GLOBAL_H #define GLOBAL_H extern int g_value; #endif // global.cpp #include "global.h" int g_value = 10; // main.cpp #include "global.h" #include <iostream> int main() { std::cout << g_value << std::endl; return 0; } 2. 使用单例模式 使用单例模式来创建一个全局变量,可以确保全局变量只被创建一次,同时可以方便地在多个文件中使用该变量: // singleton.h #ifndef SINGLETON_H #define SINGLETON_H class Singleton { public: static Singleton& instance(); int value() const; void setValue(int newValue); private: Singleton(); Singleton(const Singleton&); Singleton& operator=(const Singleton&); int m_value; }; #endif // singleton.cpp #include "singleton.h" Singleton::Singleton() : m_value(0) { } Singleton& Singleton::instance() { static Singleton s; return s; } int Singleton::value() const { return m_value; } void Singleton::setValue(int newValue) { m_value = newValue; } // main.cpp #include "singleton.h" #include <iostream> int main() { std::cout << Singleton::instance().value() << std::endl; Singleton::instance().setValue(10); std::cout << Singleton::instance().value() << std::endl; return 0; } 3. 使用全局变量使用全局变量类可以方便地定义和管理全局变量,同时避免了使用全局变量时可能出现的一些问题: // globalvariable.h #ifndef GLOBALVARIABLE_H #define GLOBALVARIABLE_H template<typename T> class GlobalVariable { public: static T& instance(); private: GlobalVariable(); GlobalVariable(const GlobalVariable&); GlobalVariable& operator=(const GlobalVariable&); static T m_instance; }; template<typename T> T GlobalVariable<T>::m_instance; template<typename T> T& GlobalVariable<T>::instance() { return m_instance; } #endif // main.cpp #include "globalvariable.h" #include <iostream> int main() { GlobalVariable<int>::instance() = 10; std::cout << GlobalVariable<int>::instance() << std::endl; return 0; } 以上是Qt多文件全局变量使用方法,根据实际需要选择合适的方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天使也有爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值