Results 1 to 7 of 7

Thread: Function to convert std::string to QString

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Function to convert std::string to QString

    Hi there!

    I have this toQString function
    Qt Code:
    1. QString Diet::toQString()const
    2. {
    3.  
    4. QString qName(this->name.c_str());
    5. QString qUserName(this->userName.c_str());
    6. QString qDate(this->date.c_str());
    7. QString kcal;
    8. QString carb;
    9. QString protein;
    10. QString lipid;
    11.  
    12. carb.setNum(this->carb);
    13. protein.setNum((this->protein));
    14. lipid.setNum(this->lipid);
    15. kcal.setNum(this->kcal);
    16.  
    17. QString str=qUserName;
    18. str.append(" "+qDate+" "+qName+" "+protein+" "+carb+" "+lipid+" "+kcal);
    19.  
    20. return str;
    21. }
    To copy to clipboard, switch view to plain text mode 
    I know this function works properly as it returns the qstring I want.

    Now In my other class I have this function.
    Qt Code:
    1. void DH::toQString()const
    2. {
    3.  
    4. for(int i=0;i< this->nrOfDiets;i++)
    5. {
    6. this->dh[i]->toQString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    What I would like to accomplish with these two functions is for example when I write something like this
    Qt Code:
    1. void MainWindow::on_pushButton_3_clicked()
    2. {
    3. DH dh1;
    4. ui->list2->addItem(dh1.toQString());
    5. }
    To copy to clipboard, switch view to plain text mode 
    the info from the array should be converted to QString then presented in the list widget, but I'm doing something worn because it crashes when I try this.

    Any kind of help is appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Function to convert std::string to QString

    I don't know what the title of the post has to do with your question.

    And, what is the purpose of this function?
    Qt Code:
    1. void DH::toQString()const
    2. {
    3.  
    4. for(int i=0;i< this->nrOfDiets;i++)
    5. {
    6. this->dh[i]->toQString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Seeing how you use it in:
    Qt Code:
    1. ui->list2->addItem(dh1.toQString());
    To copy to clipboard, switch view to plain text mode 
    I guess you don't want to return void but QString?

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Function to convert std::string to QString

    I don't know what the title of the post has to do with your question.

    And, what is the purpose of this function?
    doesn't the first function I posted convert std::strings to QString?

    I did something wrong when I copied that function its supposed to look like this.
    Qt Code:
    1. QString DH::toQString()const
    2. {
    3.  
    4. for(int i=0;i< this->nrOfDiets;i++)
    5. {
    6. return this->dh[i]->toQString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    But I don't think this works, bcs I think the for-loop will break when the QString returns, so only one QString will be presented, pretty unsure tho.

    the point of it is to go through the array and call the function that converts everything to QString and then return that QString.

    yeah you are right, I was just trying out something and forgot to change back.
    So yes it should be QString not void.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Function to convert std::string to QString

    The return will happen only once, if you return a QString the function will return only the value of the first string (dh[0])

    I really don't understand the logic of this function, but be careful about recursion too (if dh is a container of DH objects/pointers)

    //And if you have a container of std::string you will need to copy that in another container that contains QString (you can use the static member function of QStrin fromStdString(...))

  5. #5
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Function to convert std::string to QString

    thats what I thought.
    the logic is that usually I have a function that looks something like this.
    Qt Code:
    1. void Present::present() const
    2. {
    3. cout<<"To: "<<this->to<<" Is: "<<this->is;
    4. }
    To copy to clipboard, switch view to plain text mode 
    and In my other class with the array I have this function
    Qt Code:
    1. void Hadler::show()const
    2. {
    3. for(int i=0;i<this->nrOfPresents;i++)
    4. {
    5. this->prsnt[i]->present();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    so lets say I call show() in main it will go through my array and cout all the info in it.

    What I want to do is something similar.
    Now I have a function that gets the content from the array, converts everything to one big string and then returns that and then it presents it in my listwidget, after that I does the same thing again until all items are presented in the listwidget.

    thats why I have the following function cos I want the QString to be return as long as i is less then nrOfDiets.
    Qt Code:
    1. QString DH::toQString()const
    2. {
    3.  
    4. for(int i=0;i< this->nrOfDiets;i++)
    5. {
    6. return this->dh[i]->toQString();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Hope you understand

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Function to convert std::string to QString

    ...cos I want the QString to be return as long as i is less then...
    I understand that... but i told you that is not what happens (and it didn't even convert anything to QString)

    And assuming that would be possible (it is not) how would you "catch" the returned values - if you call a function once and then it returns a variable number of values?

    Ok, now, you can do a function that takes by reference a std::list<std::string> (it can be any container std::list is just an example) and add a QString to the QStringList for each value in the first list.

    LE:
    you can read more about recursion, if you want, because you have some confusion about it, and use the debugger to see exactly what it happening - you will understand that and you will improve debugging skills

  7. #7
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Function to convert std::string to QString

    yeah I understand also that it is not possible the way I did it, thats why I'm asking for help.

    So you mean that my first function doesn't work at all? bcs if I call that function wiht ui->list2->addItem(d1.toQString());
    the info on dh[0] will be show as a big QString.
    So it is not possible to write a function that will go through my array and every time present the info from a certain slot?

    And yeah my recursion knowledge is very limited.

Similar Threads

  1. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 02:50
  2. Replies: 8
    Last Post: 25th November 2010, 12:40
  3. How to convert string into const int ?
    By babygal in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2010, 11:57
  4. How to convert QString to std::string or char*?
    By yangyunzhao in forum Qt Programming
    Replies: 26
    Last Post: 26th August 2009, 07:52
  5. convert a string to hex
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2009, 11:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.