Python Forum
Two QlineEdit box, which interrelated
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two QlineEdit box, which interrelated
#1
Hi guys,

I have a general question here.

How can I make sure two QLineEdit box are interrelated?
I haven't code, just to get an idea here.

For example, box A and B are interrelated.
When I change box A, B will update too.

But how I change box B and update box A?
My understanding is the code is flow from box A to box B, the value from box A will overwrite box B changed by user earlier.

Should I implement something like textChanged or valueChanged?

Thank you very much.
Reply
#2
This question sounds familiar.

First I would ask myself why do I have two line edit boxes that are interrelated.

Assuming this is a good idea in your case, when do you want to "sync" the entries? While typing? When the control loses focus? When enter is pressed?

I found QLineEdit lacking and added the concept of "accept". I wanted to ignore typing and only sent a notification when the enter key was pressed or if the QLineEdit lost focus. When this happens my modified QLineEdit emits a valuechanged signal.
class LineEdit(QLineEdit):
    """QLineEdit with revert on focus out and valuechanged signal.
    valuechanged differs from textChanged in that the value does
    not change until the enter/return key is pressed.  If I lose
    focus before enter/return is pressed I perform the selected
    revert action
    """
    # Instance variables
    revert: bool    # Action taken when control loses focus
    _value: str     # Accepted text

    # This acts like an ivar but must be declared here.  Why?
    valuechanged = Signal(str)

    def __init__(self, *args, revert=True, **kv_args):
        super().__init__(*args, **kv_args)
        self._value = ''
        self.revert = revert

    def event(self, ev):
        """Override QLineEdit event method.
        Intercept tab key press. When tab pressed accept the text and
        navigate to the next control.  Also capture return key pressed
        since processing is the same as tab key.  Capture focus out
        event and revert to last accepted text
        """
        if ev.type() == QEvent.KeyPress:
            if ev.key() in (int(Qt.Key_Tab), int(Qt.Key_Return)):
                self._accepttext()
        elif ev.type() == QEvent.FocusOut:
            self._focusout()
        return super().event(ev)

    def _focusout(self):
        """Called when control loses focus.  Based on revert mode
        either revert the last accepted text, or treat as if return
        was pressed"""
        if self._value != self.text():
            if self.revert:
                self.setText(self._value)
            else:
                self._accepttext()

    def _accepttext(self):
        """Called when changed text is accepted.  Emit valuechanged signal"""
        self._value = self.text()
        self.valuechanged.emit(self._value)

    def clear(self):
        """Set to no display"""
        self.value = ''

    @property
    def value(self):
        """Accepted text"""
        return self._value

    @value.setter
    def value(self, value):
        self._value = value
        self.setText(value)
Once you how notification should work, it comes down to either using one of the available signals, or subclassing QLineEdit to act the way you want. Eventually you will call a function that updates the controls. Depending on how they are related this may be the same function for both, or separate functions for each. You will have to implement some kind of lock to prevent the A callback changing B from calling the B callback. For this I use a common variable in the callback functions.
def a_changed()
    if not lockedout
        lockedout = True
        # set value of B
        lockedout = False

def b_changed()
    if not lockedout
        lockedout = True
        # set value of A
        lockedout = False
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,731 Nov-04-2022, 09:04 PM
Last Post: malonn
  How to accept only float number in QLineEdit input ism 5 28,114 Jul-06-2021, 05:23 PM
Last Post: deanhystad
  Simple printing the text for a QLineEdit thewolf 16 7,654 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 7,741 Oct-21-2020, 02:35 PM
Last Post: arbiel
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 7,087 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 3,156 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,250 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,595 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 7,636 Aug-15-2019, 02:37 PM
Last Post: Denni
  How to validate multiple QLineEdit widgets without addressing them separately? mart79 3 4,182 Aug-08-2019, 12:50 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020