Results 1 to 5 of 5

Thread: How to build Qstring from char or string?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to build Qstring from char or string?

    Wich is the fast and symple way for build a QString from a char or string value ?
    I want to pass some string to a Qdialog that wants a QString ....
    Thanks

  2. #2
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to build Qstring from char or string?

    it works for me
    Qt Code:
    1. std::string ss = "test";
    2.  
    3. QString aa(ss.c_str());
    4.  
    5. qDebug()<<aa;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: How to build Qstring from char or string?

    Quote Originally Posted by kabanek View Post
    it works for me
    Qt Code:
    1. std::string ss = "test";
    2.  
    3. QString aa(ss.c_str());
    4.  
    5. qDebug()<<aa;
    To copy to clipboard, switch view to plain text mode 
    As far as i know, that QString is going to be valid as long as the std::string will be. (it share data with that pointer returned by c_str() ) Please contradict me if i'm wrong!

    So a better solution is the static member of QString fromStdString() for std::string
    Or fromAscii(), fromLatin1() for char* string.

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to build Qstring from char or string?

    I want to use strings at Qmessagebox.
    Exists a function at QmessageBox to convert string to Qstring (that is the parameter waited by Qmessabox) ?
    I dont want to write no include if it is not neccesary.
    Thanks Kabanek & Zlatomir

  5. #5
    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: How to build Qstring from char or string?

    You don't have to include anything, both the methods i suggested are in the QString (static members of QString), if you didn't notice, in my previous post the functions names are links to documentation.

    You can use it like this:
    Qt Code:
    1. std::string str = "Text";
    2. QString inText = QString::fromStdString(str);
    3. what_ever->setText(inText);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to convert QString to std::string or char*?
    By yangyunzhao in forum Qt Programming
    Replies: 26
    Last Post: 26th August 2009, 06:52
  2. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  3. string to char*?
    By nass in forum General Programming
    Replies: 3
    Last Post: 2nd January 2007, 15:47
  4. problem converting string-char-int
    By mickey in forum General Programming
    Replies: 1
    Last Post: 9th December 2006, 23:43
  5. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

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.