PDA

View Full Version : Function to convert std::string to QString



ogward
9th April 2011, 10:07
Hi there!

I have this toQString function

QString Diet::toQString()const
{

QString qName(this->name.c_str());
QString qUserName(this->userName.c_str());
QString qDate(this->date.c_str());
QString kcal;
QString carb;
QString protein;
QString lipid;

carb.setNum(this->carb);
protein.setNum((this->protein));
lipid.setNum(this->lipid);
kcal.setNum(this->kcal);

QString str=qUserName;
str.append(" "+qDate+" "+qName+" "+protein+" "+carb+" "+lipid+" "+kcal);

return str;
}
I know this function works properly as it returns the qstring I want.

Now In my other class I have this function.

void DH::toQString()const
{

for(int i=0;i< this->nrOfDiets;i++)
{
this->dh[i]->toQString();
}
}
What I would like to accomplish with these two functions is for example when I write something like this

void MainWindow::on_pushButton_3_clicked()
{
DH dh1;
ui->list2->addItem(dh1.toQString());
}
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.

tbscope
9th April 2011, 10:45
I don't know what the title of the post has to do with your question.

And, what is the purpose of this function?

void DH::toQString()const
{

for(int i=0;i< this->nrOfDiets;i++)
{
this->dh[i]->toQString();
}
}

Seeing how you use it in:

ui->list2->addItem(dh1.toQString());
I guess you don't want to return void but QString?

ogward
9th April 2011, 11:20
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.

QString DH::toQString()const
{

for(int i=0;i< this->nrOfDiets;i++)
{
return this->dh[i]->toQString();
}
}
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.

Zlatomir
9th April 2011, 11:43
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(...) (http://doc.qt.nokia.com/latest/qstring.html#fromStdString))

ogward
9th April 2011, 12:05
thats what I thought.
the logic is that usually I have a function that looks something like this.


void Present::present() const
{
cout<<"To: "<<this->to<<" Is: "<<this->is;
}

and In my other class with the array I have this function


void Hadler::show()const
{
for(int i=0;i<this->nrOfPresents;i++)
{
this->prsnt[i]->present();
}
}
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.


QString DH::toQString()const
{

for(int i=0;i< this->nrOfDiets;i++)
{
return this->dh[i]->toQString();
}
}

Hope you understand

Zlatomir
9th April 2011, 12:16
...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 ;)

ogward
9th April 2011, 12:46
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.