以下是示例代码:/n/n// globalval.h/nclass MainWindow; // 前向声明/n/nextern MainWindow *pThis; // 声明全局变量/n/n// mainwindow.cpp/n#include /'mainwindow.h/'/n#include /'globalval.h/'/n/nMainWindow::MainWindow(QWidget *parent)/n : QMainWindow(parent)/n{/n pThis = this; // 保存实体指针到全局变量/n}/n/n/n// mainwindow.h/n#ifndef MAINWINDOW_H/n#define MAINWINDOW_H/n/n#include /n/nnamespace Ui {/nclass MainWindow;/n}/n/nclass MainWindow : public QMainWindow/n{/n Q_OBJECT/n/npublic:/n explicit MainWindow(QWidget *parent = nullptr);/n ~MainWindow();/n/nprivate:/n Ui::MainWindow ui;/n};/n/n#endif // MAINWINDOW_H/n/n// mainwindow.cpp/n#include /'mainwindow.h/'/n#include /'globalval.h/'/n/nMainWindow::MainWindow(QWidget parent)/n : QMainWindow(parent)/n , ui(new Ui::MainWindow)/n{/n ui->setupUi(this);/n pThis = this; // 保存实体指针到全局变量/n}/n/n// third.cpp/n#include /'mainwindow.h/'/n#include /'globalval.h/'/n/nint someFunction()/n{/n int value = pThis->ui->led_time->text().toInt(); // 使用全局变量/n // .../n}/n/n这样写会报一个错误/n/nd://QTProjects//JBMN//mythread.cpp:42: error: 'Ui::MainWindow MainWindow::ui' is private within this context/n../JBMN//mythread.cpp: In member function 'virtual void RunScript::run()':/n../JBMN//mythread.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<JB_TYPE>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]/n 28 | for(int j=0;j<jbList.size();j++){/n | ~^~~~~~~~~~~~~~/n../JBMN//mythread.cpp:42:34: error: 'Ui::MainWindow MainWindow::ui' is private within this context/n 42 | int a=pThis->ui->led_time->text().toInt();/n | ^~内容:以上错误是因为在 third.cpp 中访问了 MainWindow 类中的私有成员 ui。为了避免这种错误,可以考虑使用公共接口或者信号与槽机制来实现模块之间的通信,而不是直接访问其他模块的私有成员。全局变量的使用也要谨慎,应当避免滥用。