Qt中使用全局变量的两种方式
原文地址::https://blog.****.net/u014546553/article/details/78558925
相关文章
1、QT 使用全局变量的方法----https://blog.****.net/guoqianqian5812/article/details/49913371
2、QT编程之——使用全局变量----https://blog.****.net/zhenyu5211314/article/details/26210353
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 (声明写在类和命名控件的外面)
- #ifndef CGLOBAL_H
- #define CGLOBAL_H
- extern int testValue;
- #endif // CGLOBAL_H
cglobal.cpp (在函数外面定义变量)
- #include "cglobal.h"
- int testValue=1;
调用方式
- #include "cglobal.h"
- #include <QDebug>
- qDebug()<<testValue;
- testValue=2;
- qDebug()<<testValue;