0

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

Saad Saadi
  • 1,031
  • 10
  • 26
  • What is a "1x3 Char"? Can you post an example file of a ".mat" to see how it looks like? – Boris Dalstein Sep 05 '13 at 01:46
  • name_device= ABC if i apply whos command it is showing class: char, size: 1x3 and bytes: 6..does it help? – Saad Saadi Sep 05 '13 at 01:50
  • also u can write on matlab like this name_device='ABC'..it will give you the desired variable which i am asking – Saad Saadi Sep 05 '13 at 01:50
  • Ok, tell me if I understood correctly: 1) you have a file called for instance "my_file.mat". 2) the full content of this file is `name_device= ABC`. 3) you want your C++ variable "name_device" to be equal to "ABC" after you read the file (i.e., be equal to the 3 last character of your file). 4) you want to modify this, such as it is equal for instance to "DEF" after processing. 5) you want to write back this to the file "my_file.mat" so that its content is now `name_device= DEF`. – Boris Dalstein Sep 05 '13 at 02:07
  • Little Modification: 1) you have a file called for instance "my_file.mat". 2) the full content of this file is name_device= ABC. 3) you want your QString variable "name_device" to be equal to "ABC" after you read the file (i.e., be equal to the 3 last character of your file). 4) you want to modify this, such as it is equal for instance to "DEF" after processing. 5) you want to write back this QString variable to the file "my_file.mat" same as i read it (i.e Char) so that its content is – Saad Saadi Sep 05 '13 at 02:16
  • @Boris..i did it..thanks a lot for your concern.. – Saad Saadi Sep 05 '13 at 03:38

1 Answers1

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.

brunocodutra
  • 2,329
  • 17
  • 23
  • Dear brunocodutra..thanks for your reply..i tried to do this by your method but getting an erro..Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'QString' to 'const char *...can you please guide on this. thanks – Saad Saadi Sep 05 '13 at 02:37
  • @SaadSaadi you are passing an instance of `QString` to `std::strcmp`, which expects `char const *`. Please note, that all `std::strcmp` does is compare two char arrays, what exactly are you trying to do? – brunocodutra Sep 05 '13 at 02:41
  • How about first reading the char from .mat file into QChar and then transferring QChar to QString...that looks more easily..as i have char from .mat file...what you say? – Saad Saadi Sep 05 '13 at 02:42
  • @SaadSaadi I assume you are reading char by char, then all you have to do is append it to your QString using the += operator. Like this `char c = 'a'; QString qString; qString += c;`. – brunocodutra Sep 05 '13 at 02:50
  • I have `char c[1][3]`..and i am doing like this `QChar temp[3]` and then `for(int i=0;i<3;i++) temp[i]=c[0][i]` and then adding in QString like this `QString str(temp,3);`..this is done in the link which u gave me..what you think it will work..if yest then how to vica versa...from QString to Char. – Saad Saadi Sep 05 '13 at 02:54
  • @SaadSaadi You could use `std::strcpy(c[0], str.toStdString().c_str());`, assuming `c` and `str` as defined in your example. – brunocodutra Sep 05 '13 at 03:06