Important: Please read the Qt Code of Conduct -

Auto Update Model in QWidgetMapper

  • Hello
    I use QWidgetMapper with a few QlineEdit for editing data in model. when user edit fields in QLineEdit the new data will placed in the model when the enter key( or changing the focus) is pressed. I want to change this behavior so I need the data within the model change on the fly. I mean that data in model change as soon as possible user enter any key without waiting for enter key or changing widget focus.
    Is there any way to accomplish this kind of behavior?

  • Lifetime Qt Champion

    This is not supported by QDataWidgetMapper, you have to write it by your own.

  • @hadi_m
    I do this on a widget mapper for a whole bunch of QSpinBoxes. I connect to QSpinBox::valueChanged and the slot calls QDataWidgetMapper::submit() to update the model immediately. It works a treat :)

    Presumably you can do the same against QLineEdit::textEdited. Note that there is a potential little wriggle. If you're not careful, you change the model, it then changes the widget, and then that re-raises the "changed" signal and you get stuck forever. My slot reads the current value from the model and exits if the value passed to the slot is the same as that retrieved from the model. I also use Qt::QueuedConnection to prevent the slot causing changes while still within the original signal call, which was a problem. These may be because I am using QSpinBoxes, may not apply when using QLineEdit::textEdited (which is a different signal from QLineEdit::textChanged which will be emitted when you change the value in the model programmatically and it reflects back to the widget).

Log in to reply