[SOLVED] PySide strings returned from widgets have extra blank bytes between the letters



  • Hi,
    I built pyside using the buildscripts on CentOS 6.5 and Qt 4.8 (installed using these repos http://www.qtcentre.org/threads/56495-How-to-install-4-8-on-CentOS-6) on a virtual environment using Python 2.7

    I tested an application that works perfectly fine on Windows and OSX and found that whenever I printed a string from a widget (ie. comboBox.currentText() where comboBox is a QComboBox) I was missing half the string.

    Checking the length of the returned object I found that it had the correct length, but uppon further inspection I realized it had an extra blank byte between each letter. For example, a QLineEdit with the text "hello" would only print "hel".

    Printing the string as hex bytes using:

    @print ":".join(['%0X' % ord(b) for b in myString])@

    resulted in something like 68:0:65:0:6C, where indeed there is a 0 byte between each character.

    The GUI is built using pyside-uic from ui files created in the Creator.

    This sounds like I'm getting two byte unicode characters, but why am I only getting half the bytes?

    Am I missing something that needs to be configured at the app level?

    Please help and thanks for any info in advance!



  • Here is a trivial example that prints text from a QLineEdit.
    The QLineEdit text is set from a regular python string.
    When printing the original text and the text read from QLineEdit.text() they print out different things.

    @
    import sys
    from PySide import QtGui
    from PySide import QtCore

    edit=None
    myText=str("hello how are you")

    def printSomething():

    textFromQt=edit.text()
    print "\n---%s: %i---"%(textFromQt,len(textFromQt))
    for l in textFromQt:
    print '%s : %0X' % (l, ord(l) )

    print "\n\n---%s: %i---"%(myText,len(myText))
    for l in myText:
    print '%s : %0X' % (l, ord(l) )

    if name == "main":
    app = QtGui.QApplication(sys.argv)
    wnd=QtGui.QDialog()
    layout= QtGui.QVBoxLayout(wnd)
    btn=QtGui.QPushButton("push", wnd)
    edit=QtGui.QLineEdit(myText)
    layout.addWidget(edit)
    layout.addWidget(btn)

    btn.clicked.connect(printSomething)
    wnd.show()
    sys.exit(app.exec_())
    @

    And the output when pushing the button is:

    @
    ---hello how: 17---
    h : 68
    : 0
    e : 65
    : 0
    l : 6C
    : 0
    l : 6C
    : 0
    o : 6F
    : 0
    : 20
    : 0
    h : 68
    : 0
    o : 6F
    : 0
    w : 77

    ---hello how are you: 17---
    h : 68
    e : 65
    l : 6C
    l : 6C
    o : 6F
    : 20
    h : 68
    o : 6F
    w : 77
    : 20
    a : 61
    r : 72
    e : 65
    : 20
    y : 79
    o : 6F
    u : 75
    @

    Thanks for any help!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since it might be low level problem, you should also post your question on the pyside mailing list.



  • Ok, I found the solution.

    CentOS comes with Python 2.6 and I had created my virtual environment with Python 2.7 compiled from source.

    I recompiled Python 2.7 adding --enable-unicode=ucs4 when configuring the build. The default is to compile with --enable-unicode=ucs2

    so in the end I compiled like this:

    @
    ./configure --prefix /usr/local --enable-shared --enable-unicode=ucs4
    make
    make altinstall
    @

    I recreated my virtualenv with the newly compiled python and everything works as it should!


  • Lifetime Qt Champion

    That might be something to talk about with the pyside devs



  • [quote author="SGaist" date="1406317872"]That might be something to talk about with the pyside devs[/quote]

    I'll share my findings with the list. Cheers!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.