在我之前的问题之后,我的主窗口上有我的小部件。但是这种情况并没有带来任何数据:datagridview没有列和树视图是不可见的或没有我的硬编码项。这是我的代码:
app.py
#!/usr/bin/env python
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, qApp, QWidget, QMainWindow, QGridLayout, QMenuBar, QAction, QToolBar, QStatusBar
from views import Main
class TerraSoft(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('TerraSoft')
self.setWindowState(Qt.WindowMaximized)
exitAct = QAction(QIcon('exit24.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit)
fileMenu = self.menuBar().addMenu('File')
fileMenu.addAction(exitAct)
toolbar = self.addToolBar('Main')
toolbar.addAction(exitAct)
main = Main()
self.setCentralWidget(main)
self.statusBar().showMessage('Bienvenue dans TerraSoft')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TerraSoft()
ex.show()
sys.exit(app.exec_())
.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, qApp, QAction, QSplitter, QMenuBar, QToolBar, QGridLayout, QStatusBar
from Family.views import FamilyTreeView
from Specie.views import EventsTableView
class Main(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
familyTreeView = FamilyTreeView(self)
eventsTableView = EventsTableView(self)
HSplitter = QSplitter(Qt.Horizontal)
HSplitter.addWidget(familyTreeView)
HSplitter.addWidget(eventsTableView)
grid = QGridLayout()
grid.addWidget(HSplitter)
self.setLayout(grid)
模块:Family.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView
families = [
("Craspedocephalus", [
("puniceus", []),
("trigonocephalus", [])
]),
("Trimeresurus", [
("albolabris", [])
]),
("Elapidé", [])
]
class FamilyTreeView(QWidget):
"""description of class"""
def __init__(self, *args):
QWidget.__init__(self, *args)
self.familyList = QTreeView()
self.familyList.setMaximumWidth(300)
self.model = QStandardItemModel()
self.addItems(self.model, families)
self.familyList.setModel(self.model)
self.model.setHorizontalHeaderLabels([self.tr("Familles")])
def addItems(self, parent, elements):
for text, children in elements:
item = QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
module:specie.views.py
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTreeView
class EventsTableView(QTableWidget):
"""description of class"""
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.eventsTable = QTableWidget()
self.eventsTable.setColumnCount(3)
self.eventsTable.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))
enter code here
希望你能再次帮助我,
感谢每次前进!
答案 0 :(得分:1)
不确定为什么要从 QWidget 派生 FamilyTreeView 。我直接从 QTreeView 中找到了一个解决方案。您的 addItems 方法正常工作,但我使用 QStandardItem 的 invisibleRootItem 成员作为上级父级(即根)。
class FamilyTreeView(QTreeView):
"""description of class"""
def __init__(self, *args):
QTreeView.__init__(self, *args)
the_model = QStandardItemModel()
the_model.setHorizontalHeaderLabels([self.tr("Familles")])
self.addItems(the_model.invisibleRootItem(), families)
self.setModel(the_model)
def addItems(self, parent, elements):
for text, children in elements:
item = QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
您的EventsTableView类也存在一些问题。它继承自QTableWidget(即是 QTableWidget),但定义了一个内部QTableWidget,这是不必要的。这是一个更好的版本:
class EventsTableView(QTableWidget):
"""description of class"""
def __init__(self, *args):
QTableWidget.__init__(self, *args)
self.setColumnCount(3)
self.setHorizontalHeaderLabels(('Date', 'Catégorie', 'Description'))