1
I’m making the code to view my sqlite3 table in Qtableview, but when I put to view the table, at the time of running the code, this cited error arose, but I could not find how to solve it...
Follow the code to search in the created table:
def consulta_nutri():
bank = Bank()
c = bank.conn.cursor()
n = 'SELECT * FROM nutricionistas'
c.execute(n)
resultados = c.fetchall()
num_colunas = len(c.description)
nome_colunas = [i[0] for i in c.description]
dados = (resultados, nome_colunas)
return dados
Follow the code to view the table in the frame:
dados = b.consulta_nutri
self.modelo = CustomTableModel(dados)
self.tabela = QTableView(self.frm_relatorio)
self.tabela.setGeometry(0, 0, 630, 600)
self.tabela.setModel(self.modelo)
Follows the code:
from PySide2.QtCore import Qt, QAbstractTableModel, QModelIndex
from PySide2.QtGui import QColor
class CustomTableModel(QAbstractTableModel):
def __init__(self, data=None):
QAbstractTableModel.__init__(self)
self.meus_dados = data[0]
self.minhas_colunas = data[1]
self.load_data(self.meus_dados)
def load_data(self, dados):
self.numero_linhas = len(dados)
self.numero_colunas = len(dados[0])
def rowCount(self, parent=QModelIndex):
return self.numero_linhas
def columnCount(self, parent=QModelIndex):
return self.numero_colunas
def HeaderData(self, section, orientation, role):
if role != Qt.DisplayRole:
return None
if orientation == Qt.Horizontal:
return self.minhas_colunas[section].upper()
else:
return section
def data(self, index, role = Qt.DisplayRole):
column = index.column()
row = index.row()
if role == Qt.DisplayRole:
return self.meus_dados[row][column]
elif role == Qt.TextAlignmentRole:
return Qt.AlignLeft
elif role == Qt.BackgroundRole:
return QColor(Qt.white)
return None