Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Why can not take QLabel std:string?
QtWS25 Last Chance

[solved] Why can not take QLabel std:string?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lord of Noob
    wrote on last edited by Lord of Noob
    #1

    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?

    J 1 Reply Last reply
    0
    • L Lord of Noob

      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?

      J Offline
      J Offline
      jjan
      wrote on last edited by jjan
      #2

      @Lord-of-Noob

      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));
      
      1 Reply Last reply
      1
      • L Offline
        L Offline
        Lord of Noob
        wrote on last edited by
        #3

        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.

        K 1 Reply Last reply
        0
        • L Lord of Noob

          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.

          K Offline
          K Offline
          koahnig
          wrote on last edited by koahnig
          #4

          @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());
          

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            @Lord-of-Noob Keep in mind that QString supports UNICODE but std::string don't. This is why there're no automatic conversion operators

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            2
            • L Offline
              L Offline
              Lord of Noob
              wrote on last edited by
              #6

              thanks koahnig & mcosta. This solves the topic for me.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved