Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top