[英]How to access variables in main.cpp from function in mainwindow.cpp?
I click button and want change some variables in main.cpp.我单击按钮并希望更改 main.cpp 中的一些变量。 How access it from function in mainwindow.cpp.如何从 mainwindow.cpp 中的函数访问它。
void MainWindow::on_spinBox_valueChanged(int coef)
{
//here I need x=coef;
//x is in main.cpp
}
As explained in the comments, it is better to change the architecture of your program.正如评论中所解释的,最好更改程序的架构。 However, if you still want to opt for a simple solution, you can use the external variables:但是,如果您仍然想选择一个简单的解决方案,您可以使用外部变量:
Declare x
in your file main.cpp
out of main function:在main.cpp
文件之外的 main 函数中声明x
:
#include ...
int x;
int main ()...
In your mainwindow.cpp
, declare your x
as extern
in global scope:在您的mainwindow.cpp
,将您的x
声明为全局范围内的extern
:
extern int x;
void MainWindow::on_spinBox_valueChanged(int coef)
{
x = coef;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.