Results 1 to 9 of 9

Thread: QString probs...

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QString probs...

    I have that piece of code

    Qt Code:
    1. from PyQt4 import *
    2. from PyQt4.QtCore import *
    3. a = QString("攢ð")
    4. print unicode(a)
    To copy to clipboard, switch view to plain text mode 

    this won't print the same chars (攢ð)...
    If I do "print unicode(a).encode("utf-8") I won't get 攢ð, instead of that I'll get exactly the same thing as the another print.

    What should I do?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString probs...

    supposing your file encoding is utf8, use
    Qt Code:
    1. a = QString::fromUtf8("攢ð");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString probs...

    That works great in console, however, I think that this wasn't what I was looking for.
    I have a QTextEdit and I get the text with .toPlainText()
    Then I have to pass that string to str() but it will fail with that error:

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    Any ideas? Maybe .toPlainText() return unicode and not utf-8 ?

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString probs...

    Pass the string with QString::toUtf8().
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString probs...

    msg = QString.fromUtf8(self.ui.inputWidget.toPlainText() )
    message = str(QString.toUtf8(msg))
    message.content.encode("utf-8") << This will fail with UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString probs...

    Quote Originally Posted by alexandernst View Post
    Maybe .toPlainText() return unicode and not utf-8 ?
    It's utf8, because Qt handles all as utf by default. But of what str function are you talking about? And what type is message? But anyway maybe QString::toLocal8Bit() will help you.

  7. #7
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString probs...

    Lets see...

    I get the text from the QTextEdit widget this way:

    msg = QString.fromUtf8(self.ui.inputWidget.toPlainText() )

    So, "msg" is a QString utf8 encoded, right?

    Next, I want to get only the string from that QString object, so I do

    message = str(QString.toUtf8(msg))

    Next, I have another method that receives that "message" string and tries to do .encode("utf-8") and there's where my app fails, because message.encode("utf-8") says that "ascii" codec can't decode byte.......

    And that were I'm stuck.

  8. #8
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString probs...

    Ok... lets make it even easier...

    I'll just get the text this way:

    msg = self.ui.inputWidget.toPlainText()

    So, "msg" will be a QString encoded in utf8. How can I convert that string to python str? (without loosing data)

    EDIT: I would like to have the text in a python str, so, whenever I do "str(message)" or "message.encode("utf-8")" it will work without problems.
    Last edited by alexandernst; 30th August 2009 at 16:44.

  9. #9
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString probs...

    Nah, forget my last 2 posts.
    I just want to get that text from the QTextEdit and be able to do "print message.encode("utf-8")" and see exactly what I typed. Is that possible?

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 20:49
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.