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. How to fix string index out of range errror
Forum Update on Monday, May 27th 2025

How to fix string index out of range errror

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 493 Views
  • 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.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    I'm trying to display details in a QLabel but when I try I get the following error, string index out of range. Below is the code that generated the error, I would appreciate assistance. Thanks

    self.ui.tableWidget.cellClicked.connect(self.singleClick)
       
    def singleClick(self):
         
           try:
               data = self.ui.tableWidget.currentItem().text()
               id = data.split("-")[0]
    
               query = ("SELECT * FROM temporary_staff WHERE id_no=?")
               person = cur.execute(query, (id,)).fetchone()  ######### single item tuple=(1,)
               label_pixmap = QPixmap("img/" + data[16])
               
               self.ui.label_15.setPixmap(QPixmap(label_pixmap))
               self.ui.label_15.repaint()
               self.ui.label_2.setText(data[1])
               self.ui.label_4.setText(data[3])
               self.ui.label_6.setText(data[4])
               self.ui.label_8.setText(data[12])
               self.ui.label_10.setText(data[15])
               
           except Exception as e:
               print(e)
    
    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      You assume that is at least 15 char width. That is the issue with assumptions: they are often incorrect. Don't trust them, add proper checks to ensure that your next operations don't fail.

      L Offline
      L Offline
      LT-K101
      wrote on last edited by
      #3

      @SGaist Thanks I did a silly mistake I was referencing data instead of person.Like below

      self.ui.label_2.setText(person[1])
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You assume that is at least 15 char width. That is the issue with assumptions: they are often incorrect. Don't trust them, add proper checks to ensure that your next operations don't fail.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You assume that is at least 15 char width. That is the issue with assumptions: they are often incorrect. Don't trust them, add proper checks to ensure that your next operations don't fail.

          L Offline
          L Offline
          LT-K101
          wrote on last edited by
          #3

          @SGaist Thanks I did a silly mistake I was referencing data instead of person.Like below

          self.ui.label_2.setText(person[1])
          
          1 Reply Last reply
          0

          • Login

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