Setting all QLineEdits to readOnly

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 01:26:48

问题


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 QLineEdityou 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!