Preventing cursor moving after text property is set
-
On several places the question has been raised how to overcome the moving of a cursor to the end of the widget each time the text property is updated. This happens when a QLineEdit widget is connected via a QDataWidgetMapper to a model, and this model emits datachanged.
The hints on multiple places is: store the cursor position. But I think this can only be achieved by a hack, which basically attaches the affected widgets under the model to the model. Before the data changed store the QTextCursor and after the signal, apply it again.
The more practical question is: is there a way to do it in a less hacky way. Or prevent the moment from a programmatic input at all?
-
Hi and welcome to devnet,
Since you mention QDataWidgetMapper, you can use a custom property that will receive the data change and handle the cursor position the way you want it.
-
Hi and welcome to devnet,
Since you mention QDataWidgetMapper, you can use a custom property that will receive the data change and handle the cursor position the way you want it.
-
Hi and welcome to devnet,
Since you mention QDataWidgetMapper, you can use a custom property that will receive the data change and handle the cursor position the way you want it.
@SGaist this in indeed a very cool trick that works even better than I thought.
I have added a custom property text2 that was set as the user property. This also means that I do not have to change the QDataWidgetMapper addMapping function, since that one requests the user property to begin with.
Thank you very much for this sneaky/clever trick as @JonB points out :-)
-
S SGaist has marked this topic as solved on