[solved] Why can not take QLabel std:string?
-
Hello following code does not work:
<pre><code>
std:string text = "Hello World";
QLabel *showtext = new QLabel(this);
showtext->setText(text);
</code></pre>I mean it is a bit unexpected for me that Qt does not support standard classes.
And I would like to know the reasons behind it.Is it easy to add a overload setText function?
-
Hello following code does not work:
<pre><code>
std:string text = "Hello World";
QLabel *showtext = new QLabel(this);
showtext->setText(text);
</code></pre>I mean it is a bit unexpected for me that Qt does not support standard classes.
And I would like to know the reasons behind it.Is it easy to add a overload setText function?
You can convert the std::string into an QString. QLabel only handles QString's. Try it like so:
showtext->setText(QString::fromUtf8(text.c_str()));
EDIT:
An easier way would be also:
showtext->setText(QString::fromStdString(text));
-
Thanks, for the Pragmatic answer!
It solves the Problem without understanding the backround.Maybe I asked in the wrong categroy. I am more asking from the libary view then from the usage view.
Still, I realy appreciate your quick answer.
-
Thanks, for the Pragmatic answer!
It solves the Problem without understanding the backround.Maybe I asked in the wrong categroy. I am more asking from the libary view then from the usage view.
Still, I realy appreciate your quick answer.
@Lord-of-Noob
The background is that the std::string is a standard template class. The implementation of a string within Qt is based on QString which is different. Therefore, QLabel uses of course QString for internal consistency. QString does not provide operators for direct (hidden) conversion. However, QString supports very direct conversion with fromStdString and toStdString.Both versions supplied by
@jjan said:You can convert the std::string into an QString. QLabel only handles QString's. Try it like so:
An easier way would be also:showtext->setText(QString::fromStdString(text));
are possible. The alternative is better used as :
showtext->setText(text.c_str());
-
@Lord-of-Noob Keep in mind that
QString
supports UNICODE butstd::string
don't. This is why there're no automatic conversion operators -
thanks koahnig & mcosta. This solves the topic for me.