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?

    I can't edit main post O,o.

    So more data and tests: Assuming we have only 1 record:
    ['03/08/2017', 'Otras nuevas facturas', '16/08/2017', 333333, '11123', 'No', 'No', 'No']
    The last 2 columns will appear as invisible, because they share the same value.

    With the next example, column 3 appears empty... because 16/08/2017 is also the data in column 1.
    ['16/08/2017', 'Otras nuevas facturas', '16/08/2017', 333333, '11123', 'No', 'Yadda', 'Test']

    Per row, it somehow only shows the first value if there is duplicated data. Why?

    Apparently this problem has been here for years: https://stackoverflow.com/questions/...uplicate-items

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    Did you see this in the QTableWidget docs?
    If you want to enable sorting in your table widget, do so after you have populated it with items, otherwise sorting may interfere with the insertion order (see setItem() for details).
    Try disabling sorting before the load, loading,and then enabling sorting.

  3. #3
    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?

    I actually saw it, but it didn't specified "sorting won't let you add duplicated items to the same row" so I didn't tried to change.

    Now I tried the next code but the same behaviour still happens. Repeated data won't show:

    self.tablaBuscador.setSortingEnabled(False)
    self.tablaBuscador.setItem(...)
    self.tablaBuscador.setSortingEnabled(True)

    I even tried to disable sorting entirely and use a non-sorted table, but yet if there is repeated data like "something", "something", "something", the last one won't appear, instead an empty editable cell is inserted.

    Edit: Recorded video with my crap phone just to make clear the issue: https://twitter.com/Saelyth/status/898196447425855488

    Edit 2: Someone as newbie as me would expect a "enableDuplicates" setting, because I don't understand why a row can't have 2-3 cells with the same exact data. By default it should allow it, and only disable it if we specifically want.
    Last edited by Saelyth; 17th August 2017 at 16:18.

  4. #4
    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.

  5. #5
    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.