Results 1 to 10 of 10

Thread: QString problem

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QString problem

    Hi guys, this is something which is making me wild...

    This is a simple thing but I'm not able to understand why this is not happening...

    My code is something like this

    const std::string &str = "hello";
    QString s(str); //here is the error, cannot typecast std::string to QString

    I dont want to use const char* in QString constructor
    like this QString s(str.data());

    My target is to use only std::string as an argument to QString...
    Can anyone help please

    Thankx

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString problem

    try QString::fromStdString(str)

    btw I am surprised this line doesn't crash the application:
    const std::string &str = "hello";

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    Quote Originally Posted by spud View Post
    try QString::fromStdString(str)

    btw I am surprised this line doesn't crash the application:
    const std::string &str = "hello";
    Hi thankx for your reply.
    But I dont see any QString::fromStdString in Qt3.3.5...

    Please help me

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString problem

    I don't see why the following shouldn't work.

    std::string str = "hello";
    QString s(str);

    Mind the missing '&'

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    Quote Originally Posted by spud View Post
    I don't see why the following shouldn't work.

    std::string str = "hello";
    QString s(str);

    Mind the missing '&'
    Sorry that too doesnt work...Have you tried on your system...
    I get his error on my system.
    cannot typecast std::string to QString

    Thankx

  6. #6
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    Hi guys, come on
    this is a simple problem....

    Please get me some ideas
    Thankx

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    try

    Qt Code:
    1. std::string str = "hello";
    2. QString s = str;
    To copy to clipboard, switch view to plain text mode 

    It works

  8. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    I am using 3.3.6 and the following code is not giving any error

    Qt Code:
    1. std::string str = "hello";
    2. QString s(str);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    Quote Originally Posted by munna View Post
    I am using 3.3.6 and the following code is not giving any error

    Qt Code:
    1. std::string str = "hello";
    2. QString s(str);
    To copy to clipboard, switch view to plain text mode 
    This is really surprising, when I run this same code in Qt3.3.5, it works fine but when the same code is run in Qt3.3.4 it fails..Telling it cannot be typecasted.

    How is that possible???Also when I just refered Qt3.3.4 assistant there is a member called
    QString ( const std::string & str )
    Then what is the problem? Is this a bug in Qt3.3.4?
    I'm unable to understand..Please help me.

    Cannot I make this run on Qt3.3.4??
    Thankx

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString problem

    Quote Originally Posted by vermarajeev View Post
    when I run this same code in Qt3.3.5, it works fine but when the same code is run in Qt3.3.4 it fails
    Maybe that Qt 3.3.4 version was compiled without STL support?

Similar Threads

  1. plugin loading problem
    By naresh in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2007, 19:05
  2. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.