问题
I am trying to make all of my QLineEdits(I have about 150) read-only, is there a way to do this without going through and setting each individually? I was hoping I could do something like QLineEdit::setReadOnly(true);
in my constructor but I get a compiler error saying it is an illegal call of non-static member function.
Thank you in advance!
回答1:
Use QObject::findChildren()
, like this:
QList<QLineEdit*> l_lineEdits = ui->frame->findChildren<QLineEdit*>();
foreach (QLineEdit* l_lineEdit, l_lineEdits) {
l_lineEdit->setReadOnly(true);
}
In this example the ui->frame
is the parent widget of all the QLineEdits. Just change it to yours.
回答2:
What you can do is to inherit from QLineEdit
and call setReadOnly(true)
in its constructor. Now instead of creating object of QLineEdit
you create objects of your custom MyQLineEdit
. If you need to change this property dynamically then i guess there is no way but to store references to all of them in some array and traverse that to toggle this property.
来源:https://stackoverflow.com/questions/22695534/setting-all-qlineedits-to-readonly