Select text in qlabel prigrammatically
-
I have a small search function that look if labels that are children of another widget has the searched key or not, i.e, you type text in search bar and it looks for the text in qlabels. I have something like the following;
def search(self, key): for wid in self.widget().findChildren(QWidget, "preview") if key.lower() in wid.findChild(QLabel, "title").text().lower(): # select key in label print(key + " in " + str(wid.objectName())
where self is a QScrollArea and label is set to be selectable with Qt.TextSelectableByMouse and Qt.TextSelectableByKeyboard
(please ignore the loop or the method used for search.)
I need to select the searched text in qlabel if label has the text. The documentation says QLabel can be selected with setSelection() but it accepts only ints.
If the searched text is in the label, how do I select that text?
-
@tIk90wT said in Select text in qlabel prigrammatically:
If the searched text is in the label, how do I select that text?
Since you know the text of the QLabel you can use setSelection() and pass the start and length of the searched text.