Hey guys,
Thank you for your response. I can confirm that QByteArray is a working means of communication between threads (as expected). I wonder however why I haven't had any luck with QVector...
Anyway it appears that now I'll have to deal with multiple types of data to transfer between threads: double, char, string/QString etc. and I have found this resource to split strings using delimiter characters. The code is
vector<string> split(string str, string delim)
{
unsigned start = 0;
unsigned end;
vector<string> v;
while( (end = str.find(delim, start)) != string::npos )
{
v.push_back(str.substr(start, end-start));
start = end + delim.length();
}
v.push_back(str.substr(start));
return v;
}
vector<string> split(string str, string delim)
{
unsigned start = 0;
unsigned end;
vector<string> v;
while( (end = str.find(delim, start)) != string::npos )
{
v.push_back(str.substr(start, end-start));
start = end + delim.length();
}
v.push_back(str.substr(start));
return v;
}
To copy to clipboard, switch view to plain text mode
Previously, I created a commWrapper class which just contained all the different types of variables I need to transfer between threads and I would emit newData(commWrapper*); and connect it to the recipient thread, but that seemed to crash or unexpectedly exit a lot.
class commWrapper
{
public:
double a,b,c;
string derp,hurr;
};
//usage
commWrapper *hai=new commWrapper;
hai->a=69;
hai->derp="lol this is a terrible example";
emit newData(hai);
class commWrapper
{
public:
double a,b,c;
string derp,hurr;
};
//usage
commWrapper *hai=new commWrapper;
hai->a=69;
hai->derp="lol this is a terrible example";
emit newData(hai);
To copy to clipboard, switch view to plain text mode
It is worth to note that emitting a non-pointer version of commWrapper - aka, emitting newData(commWrapper); instead of newData(commWrapper*); - would not work at all and thread communication was non-existent.
Now I'm thinking that I am going to emit a QByteArray with comma-delimited data and just process it in the recipient thread with the split() function above. Hopefully that will eradicate crashes. Thoughts?
Bookmarks