[PyKDE] QLineEdit cursor problem

Doug Bell doug101 at xecu.net
Thu May 2 03:55:00 BST 2002


This is really a Qt issue, but I would like to see if anyone here has
any ideas for a work-around.  I have a QLineEdit that responds to text
input by conditionally modifying its text.  In my simple test case, it
replaces a typed 'y' with 'xxx'.  The problem is setting the cursor
position after changing the text.  Qt 3.03 insists on putting the cursor
where the typed string used to end, not where I tell it (at the end of
the modified string).

This works fine in Qt2, the problem is only with Qt3.  I wrote a rough
C++ version of my test code (my C++ is a little rusty) and got the same
results.

I've pasted my test code below.  To try it, type a string ending in a
'y' into the line edit.  It will replace the 'y' with 'xxx', but the
cursor will only be at the new string end using Qt2.

##################################################################

#!/usr/bin/env python

import sys
from qt import *

class MyEdit(QLineEdit):
    def __init__(self, parent=None, name=None):
        QLineEdit.__init__(self, parent, name)
        self.connect(self, SIGNAL('textChanged(const QString &)'), \
                     self.changeText)

    def changeText(self, text):
        newText = str(text)
        if newText[-1] == 'y':
            newText = newText[:-1] + 'xxx'
        cursorPos = len(newText)
        print 'Cursor set to', cursorPos
        self.blockSignals(1)
        self.setText(newText)
        self.blockSignals(0)
        self.setCursorPosition(cursorPos)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dlg = QWidget(None, None, Qt.WType_TopLevel)
    topLayout = QVBoxLayout(dlg, 10)
    myEdit = MyEdit(dlg)
    topLayout.addWidget(myEdit)
    app.setMainWidget(dlg)
    dlg.show()
    app.exec_loop()

######################################################################

Thanks for any input,
Doug.




More information about the PyQt mailing list