[PyQt] QTextBlockUserData unexpected behavior

Grant Roch grant.roch at gmail.com
Tue Jul 23 12:28:06 BST 2013


I'm trying to use QTextBlockUserData to hold info about blocks, but am
seeing some strange behavior.  I know there was a related issue posted in
March (
http://www.riverbankcomputing.com/pipermail/pyqt/2013-March/032457.html)
but I'm not sure if what I'm seeing is related.  Here are two cases:
1. Run the below script as is will hold onto the userData only for a short
period.  After setFirstBlock is called and it goes back into main, the
block no longer has userData with value.
2. Comment out "line set A" and uncomment "line set B".  Now userData holds
onto the value even after the button is pressed.  This is the same code so
something else is going on.

Also, are we expected to be able to subclass QTextBlockUserData in order to
add custom methods (suggested in the documentation)?  I am not seeing this
work either as they always come back as QTextClockUserData rather than my
subclass.

My versions are:
('Qt version:', '4.8.2')
('SIP version:', '4.15-snapshot-972540270afa')
('PyQt version:', '4.10.3-snapshot-05a01eef7a2e')

Thank you.


from PyQt4.QtGui import QPlainTextEdit, QPushButton, QVBoxLayout,
QTextBlockUserData,\
                        QApplication, QDialog

class Edit(QPlainTextEdit):
    def __init__(self):
        super(Edit, self).__init__()

    def testBlockData(self):
        print self.document().firstBlock().userData().value

    def setFirstBlock(self):
        userData = QTextBlockUserData()
        userData.value = 5
        self.document().firstBlock().setUserData(userData)
        self.testBlockData() # This will print 5, but it is lost immediately

def main():

    app     = QApplication([])
    dialog  = QDialog()

    editBox        = Edit()
## (line set A) Running this line doesn't hold onto the value
    editBox.setFirstBlock()
    editBox.testBlockData()

# (line set B) Running the 3 lines below does hold onto the value
#    userData = QTextBlockUserData()
#    userData.value = 5
#    editBox.document().firstBlock().setUserData(userData)

    button  = QPushButton('Test')
    button.clicked.connect(editBox.testBlockData)

    layout  = QVBoxLayout()
    layout.addWidget(editBox)
    layout.addWidget(button)

    dialog.setLayout(layout)
    dialog.show()

    app.exec_()

if __name__=="__main__":
    main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130723/8641a0f9/attachment.html>


More information about the PyQt mailing list