Results 1 to 6 of 6

Thread: PyQt5 - Fail to display QTableWidgetItem data if previous cell have same value?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2017
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQt5 - Fail to display QTableWidgetItem data if previous cell have same value?

    Stubborn me want to keep you guys informed of my newbie research advances.

    Test Script for python where you can see the bug (easier for you to execute and see yourself than me trying to explain in english):
    Qt Code:
    1. import sys
    2. from PyQt5.QtGui import QIcon
    3. from PyQt5.QtCore import pyqtSlot, Qt
    4.  
    5. class App(QWidget):
    6.  
    7. def __init__(self):
    8. super().__init__()
    9. self.title = 'PyQt5 table - pythonspot.com'
    10. self.left = 200
    11. self.top = 200
    12. self.width = 800
    13. self.height = 200
    14. self.initUI()
    15.  
    16. def initUI(self):
    17. self.setWindowTitle(self.title)
    18. self.setGeometry(self.left, self.top, self.width, self.height)
    19.  
    20. self.createTable()
    21.  
    22. # Add box layout, add table to box layout and add box layout to widget
    23. self.layout = QVBoxLayout()
    24. self.layout.addWidget(self.tableWidget)
    25. self.setLayout(self.layout)
    26.  
    27. # Show widget
    28. self.show()
    29.  
    30. def createTable(self):
    31. # Create table
    32. self.tableWidget = QTableWidget()
    33. self.tableWidget.setRowCount(2)
    34. self.tableWidget.setColumnCount(6)
    35. self.tableWidget.setHorizontalHeaderLabels(["algo", "algo", "algo", "algo", "algo", "algo"])
    36. registros = ["Celda", "Celda", "Celda", "Celda", "Celda", "Celda"]
    37. # BUGGED ROW
    38. for dato in registros:
    39. registroitem = QTableWidgetItem(dato)
    40. self.tableWidget.setItem(0, registros.index(dato), QTableWidgetItem(registroitem))
    41. # NON BUGGED ROW
    42. self.tableWidget.setItem(1,0, QTableWidgetItem("Celda"))
    43. self.tableWidget.setItem(1,1, QTableWidgetItem("Celda"))
    44. self.tableWidget.setItem(1,2, QTableWidgetItem("Celda"))
    45. self.tableWidget.setItem(1,3, QTableWidgetItem("Celda"))
    46. self.tableWidget.setItem(1,4, QTableWidgetItem("Celda"))
    47. self.tableWidget.setItem(1,5, QTableWidgetItem("Celda"))
    48.  
    49. if __name__ == '__main__':
    50. app = QApplication(sys.argv)
    51. ex = App()
    52. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Which pretty much says (your bug is on the loop). I still don't know what causes it but getting closer . Any hint?

    Edit + FIX: I am retard.... it actually seems that it was a python noob fail all this time. registros.index(dato) was always returning the first index if it's a duplicated (0). I've been coding on python 4 years and never realized about this, hence I though it was a Qt issue.

    Fix: replace "for dato in registros:" and add "for index, dato in enumerate(registro):" instead.

    My apologies guys, thread can be closed.
    Last edited by Saelyth; 18th August 2017 at 04:35.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQt5 - Fail to display QTableWidgetItem data if previous cell have same value?

    registros.index(dato) was always returning the first index if it's a duplicated (0).
    Sort of makes sense though - if you are asking to look up something by value, and that value is duplicated, how should it know you want the second one and not the first? The only way to give that information is with a second argument to index() that tells it where to start, something like index( value, startIndex ).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 14
    Last Post: 18th June 2017, 17:32
  2. Replies: 13
    Last Post: 17th July 2016, 16:20
  3. Qt for Symbian clear previous data when replace app ?
    By Dexter in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 03:25
  4. How do I display a picture on a QTableView cell?
    By danielperaza in forum Qt Programming
    Replies: 16
    Last Post: 9th April 2010, 22:04
  5. Replies: 2
    Last Post: 29th March 2010, 11:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.