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

原文地址::https://blog.csdn.net/u014546553/article/details/78558925

相关文章

1、QT 使用全局变量的方法----https://blog.csdn.net/guoqianqian5812/article/details/49913371

2、QT编程之——使用全局变量----https://blog.csdn.net/zhenyu5211314/article/details/26210353


1、使用static关键字:

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

mainwindow.h

[cpp]  view plain  copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3. #include <QMainWindow>  
  4. namespace Ui {  
  5. class MainWindow;  
  6. }  
  7. class MainWindow : public QMainWindow  
  8. {  
  9.     Q_OBJECT  
  10. public:  
  11.     explicit MainWindow(QWidget *parent = 0);  
  12.     ~MainWindow();     
  13.     static int a;      
  14.     static QString c;  
  15. private:  
  16.     Ui::MainWindow *ui;  
  17. };  
  18. #endif // MAINWINDOW_H  



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

mainwindow.cpp

[cpp]  view plain  copy
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3. #include <QtMath>  
  4. #include <qwt_plot.h>  
  5. #include <qwt_plot_curve.h> //是包含QwtPointSeriesData类的头文件  
  6. #include <qwt_plot_grid.h>  
  7.   
  8. int MainWindow::a = 100;  
  9. QString MainWindow::c = "clue";  
  10.   
  11. MainWindow::MainWindow(QWidget *parent) :  
  12.     QMainWindow(parent),  
  13.     ui(new Ui::MainWindow)  
  14. {  
  15.       
  16.     qDebug()<<"a="<< a;  
  17.   
  18.     ui->textBrowser->setText(c);  
  19. //..........................后面代码省略  

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


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关键字的全局变量,第二种会破坏封装性。



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值