[PyQt] synced line edits

Eric Frederich eric.frederich at gmail.com
Fri Sep 24 21:58:10 BST 2010


I'm having some trouble trying to figure out how to get two line edits to
constantly be in sync including cursor position.
I'm sure its simple, or a matter of creating the right connections, but I
can't seem to figure it out.
I'm hooking two line edits textChagned signal up to the other's setText
slot.

Thankfully PyQt (or Qt)'s setText is smart enough to check whether the text
actually changed before emitting another signal and not getting stuck in an
infinite loop.
The problem seems to be that setText on a QLineEdit doesn't do a similar
check before changing the cursor position.

I'd like to get two line edits synced up so that I can insert text in the
beginning, middle, or end of the string but after any letter gets typed the
cursor goes to the end of the string.

Any help is appreciated.

Thanks,
~Eric

#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui  import *

class MyForm(QDialog):
    def __init__(self, parent=None):
        super(MyForm, self).__init__(parent)
        layout = QVBoxLayout()
        le1 = QLineEdit()
        le2 = QLineEdit()
        layout.addWidget(le1)
        layout.addWidget(le2)
        self.setLayout(layout)

        self.connect(le1, SIGNAL("textChanged(QString)"), le2.setText)
        self.connect(le2, SIGNAL("textChanged(QString)"), le1.setText)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mf = MyForm()
    mf.show()
    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100924/a5ba51d4/attachment.html>


More information about the PyQt mailing list