QString to String and vica versa

拈花ヽ惹草 提交于 2019-12-13 04:37:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!