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 PyQt5 - Fail to display QTableWidgetItem data if previous cell have same value?

    Hm, I just stumbled into an issue that I don't know how to address. Somehow my QtableWidget won't show the data of 2 specific columns if the data on both is the same.
    Please check the next screenshot to see what I meant. There shouldn't be invisible values on the 2nd column at all.
    b4MAz8NLRCe7lk3jtxfMSA.png
    The first row values I doublechecked multiple times and it's "No No", but it only shows first one.
    Also doublechecked the 3rd row, the value there is "SÃ* SÃ*", but yet again second one is invisible.
    This is the big picture in case you need to check:
    M4FvuArqQWic7bBzBLGi-Q.jpg

    I fail to see why this happens.

    Qt Creator pyuic5 generated code:
    Qt Code:
    1. self.tablaBuscador = QtWidgets.QTableWidget(self.tab_buscador)
    2. self.tablaBuscador.setGeometry(QtCore.QRect(10, 40, 941, 341))
    3. self.tablaBuscador.setStyleSheet("")
    4. self.tablaBuscador.setColumnCount(0)
    5. self.tablaBuscador.setObjectName("tablaBuscador")
    6. self.tablaBuscador.setRowCount(0)
    7. self.tablaBuscador.verticalHeader().setDefaultSectionSize(25)
    8. self.tablaBuscador.setSortingEnabled(True)
    To copy to clipboard, switch view to plain text mode 

    This is the code where I create the table columns and headers:
    Qt Code:
    1. def creatabla_buscador(self):
    2. # Add columns and give them a header name
    3. nombres_de_columnas = ["Fecha Entrada", "Proveedor", "Fecha Factura", "Importe",
    4. "Cód. Factura", "Pagada", "Contabilizada", "Observaciones"]
    5. self.tablaBuscador.setColumnCount(len(nombres_de_columnas))
    6. self.tablaBuscador.setHorizontalHeaderLabels(nombres_de_columnas)
    7. # Adjust the size to fit perfectly
    8. header = self.tablaBuscador.horizontalHeader()
    9. for i in nombres_de_columnas:
    10. if i == "Observaciones":
    11. header.setSectionResizeMode(nombres_de_columnas.index(i), QtWidgets.QHeaderView.Stretch)
    12. else:
    13. header.setSectionResizeMode(nombres_de_columnas.index(i), QtWidgets.QHeaderView.ResizeToContents)
    14. # Connect what happens when you click on any cell
    15. self.tablaBuscador.cellClicked.connect(self.tablabuscador_alclickearceldas)
    To copy to clipboard, switch view to plain text mode 

    And this is the code I use to create rows filled with data requested from Sqlite3
    Qt Code:
    1. def rellenatabla_buscador(self):
    2. # Destroy all table data in order to replace it
    3. self.tablaBuscador.setRowCount(0)
    4. # Find the information
    5. registros = utilidades_db.buscatodoslosregistros("Recibos",
    6. "SELECT Fechaentrada, Proveedor, Fechafactura, "
    7. "Importefactura, Codigofacturadelproveedor, Pagada, "
    8. "Contabilizar, Observaciones FROM FacturasRecibidas "
    9. "WHERE Proveedor LIKE ?",
    10. (self.comboBuscador_proveedor.currentText(),), True)
    11. # Order them by index 0
    12. registros = utilidades.ordena_registros_por(registros, 0)
    13. # Create the new rows and add the items
    14. for registro in registros:
    15. self.tablaBuscador.insertRow(self.tablaBuscador.rowCount())
    16. fila = self.tablaBuscador.rowCount() - 1
    17. for dato in registro:
    18. registroitem = QtWidgets.QTableWidgetItem(utilidades.ensure_its_str(dato))
    19. registroitem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
    20. registroitem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
    21. self.tablaBuscador.setItem(fila, registro.index(dato), registroitem)
    To copy to clipboard, switch view to plain text mode 

    And I fire them together upon selecting a value on the QComboBox above it, no weird stuff in between:
    Qt Code:
    1. self.creatabla_buscador()
    2. self.rellenatabla_buscador()
    To copy to clipboard, switch view to plain text mode 

    Notes:
    Explaining possibly confusing functions.
    ensure_its_str = It just check if the data proved is a string. If not, it converts that data as a string: str(data)
    buscatodoslosregistros = Just a wrapper for "connect to database, use cursors, disconnect to database".
    ordena_registros_por = It just re-order the list of items on an specific index.
    Last edited by Saelyth; 16th August 2017 at 20:12.

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

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

  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?

    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 17:18.

  5. #5
    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 05:35.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    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, 18:32
  2. Replies: 13
    Last Post: 17th July 2016, 17:20
  3. Qt for Symbian clear previous data when replace app ?
    By Dexter in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 04: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, 23:04
  5. Replies: 2
    Last Post: 29th March 2010, 12: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.