Qt中使用全局变量的两种方式

1、使用static关键字:

头文件声明:声明为public类型变量

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();   
    static int a;    
    static QString c;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H



源文件定义:注意这里的变量定义,一定要写在函数的外面

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMath>
#include <qwt_plot.h>
#include <qwt_plot_curve.h> //是包含QwtPointSeriesData类的头文件
#include <qwt_plot_grid.h>

int MainWindow::a = 100;
QString MainWindow::c = "clue";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    qDebug()<<"a="<< a;

    ui->textBrowser->setText(c);
//..........................后面代码省略

调用方式:在函数里面调用全局变量


2、使用extern关键字:

cglobal.h  (声明写在类和命名控件的外面)

[cpp]  view plain  copy
  1. #ifndef CGLOBAL_H  
  2. #define CGLOBAL_H  
  3. extern int testValue;  
  4. #endif // CGLOBAL_H  

cglobal.cpp  (在函数外面定义变量)

[cpp]  view plain  copy
  1. #include "cglobal.h"  
  2.   
  3. int testValue=1;  

调用方式

[cpp]  view plain  copy
  1. #include "cglobal.h"  
  2. #include <QDebug>  
  3.   
  4. qDebug()<<testValue;  
  5. testValue=2;  
  6. qDebug()<<testValue;  
墙裂推荐第一种使用static关键字的全局变量,第二种会破坏封装性。




  • 36
    点赞
  • 169
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Qt将局部变量变为全局变量的方法有很多种,以下是其两种常见的方法: 1. 在头文件声明变量 在需要使用全局变量的地方,可以在头文件声明该全局变量,这样其他文件就可以通过包含该头文件来使用全局变量。 例如,在头文件声明一个全局变量: ```cpp // global.h #ifndef GLOBAL_H #define GLOBAL_H extern int globalVariable; #endif // GLOBAL_H ``` 在需要使用全局变量的源文件包含该头文件,并定义该全局变量: ```cpp // main.cpp #include "global.h" int globalVariable = 0; int main() { // 使用globalVariable return 0; } ``` 2. 使用单例模式 单例模式是一种常见的实现全局变量的方法,在Qt也可以使用单例模式来实现全局变量。 例如,定义一个名为Global的单例类,在该类定义全局变量: ```cpp // global.h #ifndef GLOBAL_H #define GLOBAL_H class Global { public: static Global& instance(); int globalVariable() const; void setGlobalVariable(int value); private: Global(); Global(const Global&); Global& operator=(const Global&); private: int m_globalVariable; }; #endif // GLOBAL_H ``` 在实现文件实现该单例类: ```cpp // global.cpp #include "global.h" Global& Global::instance() { static Global global; return global; } int Global::globalVariable() const { return m_globalVariable; } void Global::setGlobalVariable(int value) { m_globalVariable = value; } Global::Global() : m_globalVariable(0) { } Global::Global(const Global&) { } Global& Global::operator=(const Global&) { return *this; } ``` 在需要使用全局变量的地方,可以通过单例类的实例来获取和设置该全局变量: ```cpp // main.cpp #include "global.h" int main() { Global& global = Global::instance(); global.setGlobalVariable(0); int variable = global.globalVariable(); // 使用variable return 0; } ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值