View Full Version : Multiple setAttribute()

15th March 2013, 12:48
Is it possible to set an attribute for all the widgets at once or do I have to call setAttribute() on each of the instance?

For example, is it possible to set Qt::WA_MacShowFocusRect for all QLineEdit (for already created and also for yet to be created instances) or do I have to manually set the attribute on each lineEdit?

17th March 2013, 14:34
If all of them have the same parent, you can use QObject::findChildren then loop through the list and call setAttribute() on each child.

17th March 2013, 16:06
You could consider creating a QProxyStyle subclass and implement polish(QWidget*) such that it applies that attribute to all QLineEdit instances it gets


17th March 2013, 23:42
Subclass QLineEdit, put a setAttribute() call in the constructor, and use that class instead of QLineEdit when you create your widgets. You can catch line edits created in item views using QItemEditorFactory. If you don't create them with the wrong properties then you don't need to try to retrospectively fix them.

17th March 2013, 23:49
I'd go for Kevin's solution. It seems universal. What Chris suggests requires intervention for each and every line edit created and there is no control over those that come from some external code. polish() on the other hand will catch those too.

18th March 2013, 00:14
Thank you all for the suggestion, I'll try to go with polish() then.

18th March 2013, 00:42
There are some caveats in the QProxyStyle docs if you intend doing something similar for other types of control or are using a non-Qt supplied style.

18th March 2013, 00:49
Thanks for the heads-up, I think I'll use only QLineEdit for now...