问题
I am doing one project in which i have to use QT software. I have one qt variable like
QString name_device;
I am reading one .mat file using matIO library who has 1x3 Char variable with the similar name. Can anyone please tell me that how i can transfer mat's 1x3 Char variable into QString varaible? and also after transferring into QString, i will process it and after processing i want to again save it in .mat file for which i again need to do a transferring from QString to 1x3 Char.
It will be very helpful for me.
Thanks
回答1:
There are several ways one can initialize a QString, including constructing it directly from a char array.
To go back from a QString to a char array, one easy way would be to convert it to a std::string, using the method QString::toStdString() and then to a char array using the method std::string::c_str().
For example:
#include <QString>
#include <cstring>
#include <cassert>
int main()
{
char str1[] = "abc";
QString qString = str1;
char const* str2 = qString.toStdString().c_str();
assert(std::strcmp(str1, str2) == 0);
return 0;
}
Note however this simple example assumes UTF-8 encoding. Please go through the reference manual which I linked here for a more detailed description of QString.
来源:https://stackoverflow.com/questions/18626426/qstring-to-string-and-vica-versa