Show custom column names in the tableview

This commit is contained in:
2024-11-27 09:09:56 +01:00
parent 3df853b5c9
commit 4a5545ce18
3 changed files with 29 additions and 13 deletions

View File

@@ -105,7 +105,7 @@ Item {
TableView TableView
{ {
property var customWidths: [0.2, 0.5, 0.3] property var customWidths: [0.2, 0.5, 0.3, 05, 0.2, 0.2]
property real newWidth: 0 property real newWidth: 0
id: testTable id: testTable
@@ -190,7 +190,7 @@ Item {
onClicked: onClicked:
{ {
dbm.onRowClicked(row) bm.onRowClicked(row)

View File

@@ -2,36 +2,52 @@
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Slot from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Slot
CUSTOMER_COLUMN_NAMES = \
{
"userid": "Identification",
"username": "Benutzername",
"password": "Passwort",
"enabled": "Aktiv",
"roleid": "Rolle",
"gecos": "Information"
}
class BusinessModel(QAbstractTableModel): class BusinessModel(QAbstractTableModel):
def __init__(self, con): def __init__(self, con, cols):
super().__init__() super().__init__()
self.con = con self.__con = con
self.__visible_columns = cols
self.__data = self.__getData() self.__data = self.__getData()
def __getData(self): def __getData(self):
cursor = self.con.cursor() cursor = self.__con.cursor()
cursor.execute("SELECT * FROM users") cursor.execute("SELECT * FROM users")
return cursor.fetchall() self.__all_cols = [desc[0] for desc in cursor.description]
rows = cursor.fetchall()
return rows
def rowCount(self, parent= QModelIndex()): def rowCount(self, parent= QModelIndex()):
return len (self.__data) return len (self.__data)
def columnCount(self, parent= QModelIndex()): def columnCount(self, parent= QModelIndex()):
return 3 return len(self.__visible_columns)
def data(self, index, role= Qt.DisplayRole): def data(self, index, role= Qt.DisplayRole):
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
row, col = index.row(), index.column() # row = index.row()
return self.__data[row][col] row = self.__data[index.row()]
col_name = self.__visible_columns[index.column()]
col_index = self.__all_cols.index(col_name)
return row[col_index]
return None return None
def headerData(self, section, orientation, role= Qt.DisplayRole): def headerData(self, section, orientation, role= Qt.DisplayRole):
header= ["ID", "Kundenname", "Ort"] #header= ["ID", "Benutzername", "Passwort", "Information", "Rolle", "Aktiv"]
if orientation == Qt.Horizontal and role ==Qt.DisplayRole: if orientation == Qt.Horizontal and role ==Qt.DisplayRole:
return header[section] #return header[section]
col_name = self.__visible_columns[section]
return CUSTOMER_COLUMN_NAMES.get(col_name, col_name)
return super().headerData(section, orientation, role) return super().headerData(section, orientation, role)
#return None #return None

View File

@@ -49,7 +49,7 @@ if __name__ == "__main__":
else: else:
dbconf = config.getConfig()['database'] dbconf = config.getConfig()['database']
DbManager(dbconf) DbManager(dbconf)
bm = BusinessModel(DbManager().getConnection()) bm = BusinessModel(DbManager().getConnection(), ["roleid", "username", "gecos"])
#print(con is con2) #print(con is con2)