Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Select text in qlabel prigrammatically

Select text in qlabel prigrammatically

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 601 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tIk90wT
    wrote on last edited by tIk90wT
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved