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:
self.
tablaBuscador = QtWidgets.
QTableWidget(self.
tab_buscador)self.
tablaBuscador.
setGeometry(QtCore.
QRect(10,
40,
941,
341))self.tablaBuscador.setStyleSheet("")
self.tablaBuscador.setColumnCount(0)
self.tablaBuscador.setObjectName("tablaBuscador")
self.tablaBuscador.setRowCount(0)
self.tablaBuscador.verticalHeader().setDefaultSectionSize(25)
self.tablaBuscador.setSortingEnabled(True)
self.tablaBuscador = QtWidgets.QTableWidget(self.tab_buscador)
self.tablaBuscador.setGeometry(QtCore.QRect(10, 40, 941, 341))
self.tablaBuscador.setStyleSheet("")
self.tablaBuscador.setColumnCount(0)
self.tablaBuscador.setObjectName("tablaBuscador")
self.tablaBuscador.setRowCount(0)
self.tablaBuscador.verticalHeader().setDefaultSectionSize(25)
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:
def creatabla_buscador(self):
# Add columns and give them a header name
nombres_de_columnas = ["Fecha Entrada", "Proveedor", "Fecha Factura", "Importe",
"Cód. Factura", "Pagada", "Contabilizada", "Observaciones"]
self.tablaBuscador.setColumnCount(len(nombres_de_columnas))
self.tablaBuscador.setHorizontalHeaderLabels(nombres_de_columnas)
# Adjust the size to fit perfectly
header = self.tablaBuscador.horizontalHeader()
for i in nombres_de_columnas:
if i == "Observaciones":
header.
setSectionResizeMode(nombres_de_columnas.
index(i
), QtWidgets.
QHeaderView.
Stretch) else:
header.
setSectionResizeMode(nombres_de_columnas.
index(i
), QtWidgets.
QHeaderView.
ResizeToContents) # Connect what happens when you click on any cell
self.tablaBuscador.cellClicked.connect(self.tablabuscador_alclickearceldas)
def creatabla_buscador(self):
# Add columns and give them a header name
nombres_de_columnas = ["Fecha Entrada", "Proveedor", "Fecha Factura", "Importe",
"Cód. Factura", "Pagada", "Contabilizada", "Observaciones"]
self.tablaBuscador.setColumnCount(len(nombres_de_columnas))
self.tablaBuscador.setHorizontalHeaderLabels(nombres_de_columnas)
# Adjust the size to fit perfectly
header = self.tablaBuscador.horizontalHeader()
for i in nombres_de_columnas:
if i == "Observaciones":
header.setSectionResizeMode(nombres_de_columnas.index(i), QtWidgets.QHeaderView.Stretch)
else:
header.setSectionResizeMode(nombres_de_columnas.index(i), QtWidgets.QHeaderView.ResizeToContents)
# Connect what happens when you click on any cell
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
def rellenatabla_buscador(self):
# Destroy all table data in order to replace it
self.tablaBuscador.setRowCount(0)
# Find the information
registros = utilidades_db.buscatodoslosregistros("Recibos",
"SELECT Fechaentrada, Proveedor, Fechafactura, "
"Importefactura, Codigofacturadelproveedor, Pagada, "
"Contabilizar, Observaciones FROM FacturasRecibidas "
"WHERE Proveedor LIKE ?",
(self.comboBuscador_proveedor.currentText(),), True)
# Order them by index 0
registros = utilidades.ordena_registros_por(registros, 0)
# Create the new rows and add the items
for registro in registros:
self.tablaBuscador.insertRow(self.tablaBuscador.rowCount())
fila = self.tablaBuscador.rowCount() - 1
for dato in registro:
registroitem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
registroitem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
self.tablaBuscador.setItem(fila, registro.index(dato), registroitem)
def rellenatabla_buscador(self):
# Destroy all table data in order to replace it
self.tablaBuscador.setRowCount(0)
# Find the information
registros = utilidades_db.buscatodoslosregistros("Recibos",
"SELECT Fechaentrada, Proveedor, Fechafactura, "
"Importefactura, Codigofacturadelproveedor, Pagada, "
"Contabilizar, Observaciones FROM FacturasRecibidas "
"WHERE Proveedor LIKE ?",
(self.comboBuscador_proveedor.currentText(),), True)
# Order them by index 0
registros = utilidades.ordena_registros_por(registros, 0)
# Create the new rows and add the items
for registro in registros:
self.tablaBuscador.insertRow(self.tablaBuscador.rowCount())
fila = self.tablaBuscador.rowCount() - 1
for dato in registro:
registroitem = QtWidgets.QTableWidgetItem(utilidades.ensure_its_str(dato))
registroitem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
registroitem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
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:
self.creatabla_buscador()
self.rellenatabla_buscador()
self.creatabla_buscador()
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.
Bookmarks