简体   繁体   English

将字符串从一个.cpp文件传输到另一个.cpp文件

[英]Transferring a string from one .cpp file to another

Good evening, I working on a project that requires me to use a string variable in one .cpp file and same string variable in the next .cpp file. 晚上好,我正在开发一个项目,要求我在一个.cpp文件中使用一个字符串变量,并在下一个.cpp文件中使用相同的字符串变量。 This is my patientdatabase.cpp: 这是我的patientdatabase.cpp:

void patientDatabase::on_tableView_view_activated(const QModelIndex &index)
{
 QString value = ui->tableView_view->model()->data(index).toString();
 patientR.connOpen();
 QSqlQuery qry;
 qry.prepare("select * from imageTable where PatientId = '"+value+"'");
if (qry.exec()){
    IVDB *x = new IVDB;
    x->show();
}

This function basically changes windows to IVBD whenever I click on patientId on the table provided in my patientDatabase and now I need the string 'value' in my IVBD.cpp 每当我点击patientDatabase中提供的表格上的patientId时,此功能基本上会将窗口更改为IVBD,现在我需要在我的IVBD.cpp中使用字符串'value'

QSqlQuery *qry = new QSqlQuery(registration.db);
qry->prepare("select * from imageTable where PatientId ='"+value+"'");    // this string 'value; is from the last .cpp file

I dont want to use global variables but stuck here and not sure how to approach it. 我不想使用全局变量但是坚持到这里并且不确定如何处理它。

If you don't want to use global variable it is better share your classes and we can think with your current design. 如果您不想使用全局变量,最好分享您的类,我们可以考虑您当前的设计。 Otherwise there is a global approach below. 否则,下面有一个全球方法。

You will define a global variable named value in a header file and you will use extern keyword while defining. 您将在头文件中定义名为value的全局变量,并在定义时使用extern关键字。 It means that there is a global value variable that is created once and every .cpp file includes that header will use same variable. 这意味着有一个全局value变量被创建一次,每个.cpp文件都包含该标头将使用相同的变量。 Let me write an example. 让我写一个例子。

Think you have common.h file defining your string value: 认为你有common.h文件定义你的字符串值:

extern std::string value;

And you have two different .cpp files they share that common value. 你有两个不同的.cpp文件,他们共享这个共同的价值。 In file1.cpp create that value . 在file1.cpp中创建该value

file1.cpp: file1.cpp:

#include "common.h"

std::string value;

void fooInFile1() {
    value = "Changed from fooInFile1!";
}

file2.cpp file2.cpp

#include "common.h"

void fooInFile2() {
    value = "Changed from fooInFile2!";
}

In this example file1.cpp and file2.cpp are affecting the same value variable. 在此示例中,file1.cpp和file2.cpp正在影响相同的value变量。

The best solution I can think of would be using signals and slots . 我能想到的最好的解决方案是使用信号和插槽 In on_tableView_view_activated you should emit a signal and catch it in IVBD.cpp . on_tableView_view_activated您应该发出一个信号并在IVBD.cpp捕获它。

Using global variables doen't sound good, though it works! 使用全局变量听起来不太好,尽管它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在cpp中将单词从一个文件复制到另一个文件 - Copying words from one file to another in cpp 从一个.cpp文件中找到文件,但从另一个.cpp文件中给出“无此文件或目录” - file found from one .cpp file, but give “No such file or directory” from another .cpp file 从另一个.cpp 文件启动一个.cpp 文件 - Start a .cpp file from another .cpp file CPP从另一个.cpp文件调用函数 - CPP Calling a function from another .cpp file 如何在另一个 cpp 文件中使用 const 对 - How to use a const pair from one cpp file in another 如何在另一个 cpp 文件中使用一个 cpp 文件中的 function? - how can I use a function from one cpp file in another cpp file? 如何从另一个 .cpp 文件中的一个 .cpp 文件调用函数? - How can I call functions from one .cpp file in another .cpp file? 使用从 one.cpp 中的 one.cpp 初始化的 object 在 another.cpp 文件中不带类 - Using an initialized object from one .cpp in another .cpp file without classess 在所有cpp文件中使用一个globe变量。如果cpp文件的一个类更改了值,我想从另一个类cpp文件访问它 - Use one globe variable in all cpp files.If one class of the cpp file changed the value,I want to access it from another class cpp file 从另一个.cpp文件的主体编译一个.cpp文件 - Compiling a .cpp file from the body of another .cpp file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM