PDA

View Full Version : Check if a window has been modified



xtal256
19th August 2011, 02:30
Quick question - if i have a form (i.e. window) with many child input widgets (e.g. text edit), can i detect if one of the widgets, and hence the form, has been modified?

QWidget::isWindowModified() seems perfect for the job, but apparently i need to set it myself!? That kind of defeats the purpose, since i would need to write the code myself to detect change. I would have thought it would be built into Qt, so the modified property gets set whenever a widget changes, and all i have to do is query the property.

So i don't suppose there is any easy way of doing this? I am going to have to connect to every single child widget's "change" signal, aren't I :(.

marcvanriet
19th August 2011, 02:49
Hi,
You can connect to all child widgets if you use findChilderen() of your parent widget.

Make a single slot function in which you set a flag that a change has occurred.
Use findChildren() to find all the child widgets.
Connect the 'change' signal to your slot for each child.

Although I can't see a single type of signal that you can use for different types of widgets...

Regards,
Marc

Santosh Reddy
19th August 2011, 05:31
You can install a key event filter on the top level widget (from) and set modified flag in there, and then propagate the event. I hope you understand the fact that that all key events may not lead to data entry / modification, but all data entry / modification will be preceded by key event.

xtal256
19th August 2011, 07:25
@marcvanriet: Good idea, if QWidget actually had a "change" signal. Unfortunately, there is only the event function, which requires you to subclass the object to override.

@Santosh: Unfortunately, that idea is not much good either, because (as you said) i can not know what key input will lead to data modification.

Would it be possible to get something like this included in a future release of Qt? Is there anywhere i can request this feature (if others haven't already)?

marcvanriet
19th August 2011, 12:53
Hi,
To continue with the findChildren() approach...

In the loop where you go over all the children, you could check what type of widget it is, cast it, and then connect the right signal to your 'changed' slot.

So if you encounter a QLineEdit connect textChanged, for QCombobox connect currentIndexChanged and editTextChanged, ... Your 'changed' slot needs no parameters, so you can connect any signal to it afaik.

Still quite some work, but you can put this in a generic function that you can use for all your dialogs. And you can share this with the community too :)

Regards,
Marc

xtal256
20th August 2011, 14:00
Well i was thinking that if i did do some ugly solution like that, then i would put it in a generic function like you said (out of sight, out of mind :)). If i end up doing something like that, i will post here for others to enjoy http://www.qtcentre.org/images/icons/icon14.png