ich hab ein Problem, nämlich hab ich einen TreeView generiert aus einer Datenbankabfrage.
Klappt auch soweit, nur beim generieren des TreeModels bleibt das rootItem sichtbar,
obwohl das oberste Item doch eigentlich das "invisible rootItem" sein soll und damit doch unsichtbar ?!
Mein Code sieht akutell so aus :
Code: Alles auswählen
Hauptprogramm.py :
class MainWindow3(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 150, 1000, 1000)
self.setWindowTitle("Project Management")
self.view = QtWidgets.QTreeView()
QueryReturn_Projects = DB_connection_Main.QueryDB('select extract(year from PDate.START_DATE)AQL_StartDate, P.IBN_PROJECT_NR, P.ECU_TYPE, P.CUSTOMER_NAME, P.PROJECT \
from Projects P \
join Project_Dates PDate on P.IBN_PROJECT_NR = PDate.IBN_PROJECT_NR \
order by P.IBN_PROJECT_NR')
self.QueryReturnString_Projects_Parent = QueryReturn_Projects.fetchall()
# eine Liste der Überschriften für die Tabelle
self.HeaderTitle = ["Start Date", "Project Number", "ECU Type", "Customer Name", "Project"]
QueryReturn_Setups = DB_connection_Main.QueryDB('select S.IBN_PROJECT_NR, S.SETUP_NUMBER, P.QUALIFICATION_TYPE_NAME, S.SETUP_STATUS \
from Setups S \
inner join QUALIFICATION_TYPES P \
on S.QUALIFICATION_TYPE_ID = P.QUALIFICATION_TYPE_ID \
order by S.IBN_PROJECT_NR')
self.QueryReturnString_Setups_Child = QueryReturn_Setups.fetchall()
self.setupModelData(self.HeaderTitle, self.QueryReturnString_Projects_Parent, self.QueryReturnString_Setups_Child)
self.view.setModel(self.model)
self.setCentralWidget(self.view)
def setupModelData(self, parent, Data_Root, Data_Child):
number_Root = 0
rootItem = Modell.MyData("oben")
Year = Data_Root[number_Root][0]
NewYear = Modell.MyData(Year, rootItem)
while number_Root < len(Data_Root):
if Data_Root[number_Root][0] != Year:
Year = Data_Root[number_Root][0]
NewYear = Modell.MyData(Year, rootItem)
Parent = Modell.MyData(Data_Root[number_Root], NewYear)
number_Child = 0
while number_Child < len(Data_Child):
if Data_Child[number_Child][0] == Data_Root[number_Root][1]:
Modell.MyData(Data_Child[number_Child], Parent)
number_Child += 1
number_Root += 1
self.model = Modell.TreeModel(rootItem, self.HeaderTitle, Data_Root, Data_Child)
if __name__== '__main__':
app = QtWidgets.QApplication(sys.argv)
Login = LoginDialog()
if Login.exec_() == QtWidgets.QDialog.Accepted:
try:
DB_connection_Main = Database(DB_User_Admin.IP, DB_User_Admin.Port, DB_User_Admin.service_name, DB_User_Admin.User, DB_User_Admin.Password)
print("DB connection opened")
except Exception as E:
print("can´t connect to DB (error) : ", E.args[0])
finally:
pass
TreeWindow=MainWindow3()
TreeWindow.show()
sys.exit(app.exec_())
Modell.py :
class MyData(object):
def __init__(self, Data, parent=None):
self.data = Data
self.children = []
self.parent = parent
if parent is not None:
parent.addChild(self)
def addChild(self, item):
self.children.append(item)
item.parent = self
def columnCount(self):
if (isinstance(self.data, list)):
return len(self.data)
elif (isinstance(self.data, tuple)):
return len(self.data)
else:
return 1
def row(self):
row = None
if self.parent is None:
row = 0
else:
for i, item in enumerate(self.parent.children):
if item == self:
row = i
break
return row
class TreeModel(QtCore.QAbstractItemModel):
def __init__(self, rootItem, HeaderTitle, Data_Parent, Data_Child):
super(TreeModel, self).__init__()
self.rootItem = rootItem
self.Header = HeaderTitle
self.Data_Parent = Data_Parent
self.Data_Child = Data_Child
def headerData(self, section, orientation, role):
if(orientation == Qt.Horizontal):
if role == Qt.FontRole:
HeaderFont = QtGui.QFont()
HeaderFont.setPointSize(12)
HeaderFont.setBold(True)
return HeaderFont
elif role == Qt.DisplayRole:
return self.Header[section] #self.rootItem.data[section]
elif role == Qt.BackgroundRole:
return QtGui.QColor("white")
elif role == Qt.ForegroundRole:
return QtGui.QColor("black")
return None
def index(self, row, column, parent=QtCore.QModelIndex()):
if parent.isValid():
parent_node = parent.internalPointer()
node = parent_node.children[row]
index = self.createIndex(row, column, node)
return index
else:
index = self.createIndex(row, column, self.rootItem)
return index
def parent(self, index):
if index.isValid():
node = index.internalPointer()
parent = node.parent
if parent is None:
parent_index = QtCore.QModelIndex()
else:
parent_index = self.createIndex(parent.row(), 0, parent)
else:
parent_index = QtCore.QModelIndex()
return parent_index
def data(self, index, role):
node = index.internalPointer()
IAmAListOrTuple = True
if (isinstance(node.data, list) or isinstance(node.data, tuple)):
IAmAListOrTuple = True
DataLength = len(node.data)
else:
IAmAListOrTuple = False
DataLength = 1
Column = index.column()
if role == QtCore.Qt.DisplayRole:
if Column >= DataLength:
pass
else:
if IAmAListOrTuple == True:
return node.data[Column]
else:
return node.data
return None
def rowCount(self, index=QtCore.QModelIndex()):
node = index.internalPointer()
if node is None:
count = 1
else:
count = len(node.children)
return count
def columnCount(self, parent=QtCore.QModelIndex()):
if parent.isValid():
return parent.internalPointer().columnCount()
else:
return 5
def addChild(self, index, child):
self.beginInsertRows(index, self.rowCount(index), self.rowCount(index)+1)
parent = index.internalPointer()
parent.addChild(child)
self.endInsertRows()
die Ausgabe ist dann folgende :
-> und dieses rootItem "oben" hätte ich gerne weg, soweit ich verstanden habe, wäre das doch sowieso unsichtbar
ich übergebe in der setupModelData das erste Item mit : rootItem = Modell.MyData("oben")
und hänge dann nach und nach die Daten an eben dieses erste Parent an (sortiert nach Jahren).
Damit ist der parent doch eigentlich none und es müsste ja das rootItem sein -> def __init__(self, Data, parent=None):
Warum wird es mir dann immer noch angezeigt ?