QT5 如何在多个cpp文件中传递变量(图文详解)

前言

  为了的可读性和移植性,我们可能需要将一个CPP文件拆分成多个文件,这时,如果在一个.cpp文件中想调用另一个.cpp中的变量该如何操作?


具体步骤

1、新建不同的.cpp项目工程文件

这里面原始参数放在ex01中,new_widget用于调用原始参数。
在这里插入图片描述

2、在ex01.cpp中声明一个外部变量

  注意:外部变量extern要放在最开始,一定不要放在class类中,否则会报错:storage class specified for 'XXX(变量名)'
在这里插入图片描述

3、在ex01.cpp中定义该变量

在这里插入图片描述

  至此,该变量的声明和定义工作已经完毕,在其他.cpp文件中,例如new_widget.cpp中,如果想调用该变量,则引用一下extern即可。

4、在其他.cpp文件中调用原始变量

在这里插入图片描述


注意事项

  要注意区分“声明”和“定义”之间的区别:
  根据C++标准的规定,一个变量声明必须同时满足两个条件,否则就是定义:
     (1)声明必须使用extern关键字;
     (2)不能给变量赋初值


extern int a; //声明

int a; //定义
int a = 0; //定义
extern int a = 0; //定义

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

完整源代码

ex01.h

#ifndef EX01_H
#define EX01_H

#include <QWidget>
#include “new_widget.h”

extern int a;

namespace Ui {
class ex01;
}

class ex01 : public QWidget
{
Q_OBJECT

public:
explicit ex01(QWidget *parent = 0);
~ex01();

private:
Ui::ex01 *ui;

private slots:
void Slot_open_widget();
};

#endif // EX01_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

new_widget.h

#ifndef NEW_WIDGET_H
#define NEW_WIDGET_H

#include <QWidget>

namespace Ui {
class new_widget;
}

class new_widget : public QWidget
{
Q_OBJECT

public:
explicit new_widget(QWidget *parent = 0);
~new_widget();

private:
Ui::new_widget *ui;
};

#endif // NEW_WIDGET_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

ex01.cpp
这里我做了一个新窗口的弹出widget->show();,不然的话显示不了qDebug()的内容

#include "ex01.h"
#include "ui_ex01.h"

int a = 15;

ex01::ex01(QWidget *parent) :
QWidget(parent),
ui(new Ui::ex01)
{
ui->setupUi(this);

connect(ui-&gt;pushButton,SIGNAL(clicked(bool)),this,SLOT(Slot_open_widget()));

}

void ex01::Slot_open_widget()
{
new_widget *widget = new new_widget();
widget->show();
}

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

new_widget.cpp

#include "new_widget.h"
#include "ui_new_widget.h"
#include <QDebug>

new_widget::new_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::new_widget)
{
ui->setupUi(this);

extern int a;
qDebug() &lt;&lt; a;

}

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行结果如图## 标题:
在这里插入图片描述

https://blog.csdn.net/weixin_33119123/article/details/113412137

在一个cpp文件里访问另一个cpp的全部变量_C++理论梳理3——深刻理解头文件的作用…

c++工程里一个文件怎么调用另外一个文件里的函数

(1)前提为,另一个文件中的函数,不能是静态函数,即不能有static修饰。
调用方法,在调用前进行声明,然后直接调用即可。

声明方法:
1 直接在调用前,写函数声明:
如调用函数为int func(int a),那么在调用前只需要
int func(int a);
这样声明后,即可使用func。

2 将声明写在头文件中。
如在名为func.h的头文件中加入
int func(int a);
然后在调用的源文件中,引用
#include “func.h”
即可调用func。

(2)举个例子吧:两个文件 main.c function.c
我们在function.c 中写一个函数 func();
与此同时,我们需要写一个.h文件来声明这个函数,即function.h
// 以下是function.h 的内容
#ifndef FUNCTION_H
#define FUNCTION_H

void func();
#endif

// 以下是function.c 的内容
#include “function.h”

void func()
{
// do something

}

这样一来,我们想要在另外一个文件,也就是main.c 中调用这个函数的时候,只需要包含它对应的头文件,就可以直接调用了。
// 以下是main.c 的内容
#include “stdio.h”
#include “function.h”

int main()
{
func();

return 0;
}

  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值