gpt4 book ai didi

python - PyQt : Adding rows to QTableView using QAbstractTableModel

转载 作者:太空狗 更新时间:2023-10-29 21:50:03 25 4
gpt4 key购买 nike

我是 Qt 编程的新手。我正在尝试制作一个简单的表格,可以通过单击按钮添加行。我可以很好地实现表格,但似乎无法将更新的数据显示在表格上。我相信我的问题源于这样一个事实,即我似乎无法使用按钮正确调用任何类型的“更改数据”方法。我在网上尝试了几种不同的解决方案,所有这些解决方案都导致了 4 年的死胡同帖子。到目前为止我所拥有的是基本结构,我只是想不出如何用新数据更新表。

这是基本 View

我已经设置了一些测试数据。

在最终实现中,表格将开始为空,我想追加行并将它们显示在表格 View 中。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWindow(QWidget):
def __init__(self):
QWidget.__init__(self)

# create table
self.get_table_data()
self.table = self.createTable()

# layout
self.layout = QVBoxLayout()

self.testButton = QPushButton("test")
self.connect(self.testButton, SIGNAL("released()"), self.test)

self.layout.addWidget(self.testButton)
self.layout.addWidget(self.table)
self.setLayout(self.layout)

def get_table_data(self):
self.tabledata = [[1234567890,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]]

def createTable(self):
# create the view
tv = QTableView()

# set the table model
header = ['col_0', 'col_1', 'col_2', 'col_3', 'col_4']
tablemodel = MyTableModel(self.tabledata, header, self)
tv.setModel(tablemodel)

# set the minimum size
tv.setMinimumSize(400, 300)

# hide grid
tv.setShowGrid(False)

# hide vertical header
vh = tv.verticalHeader()
vh.setVisible(False)

# set horizontal header properties
hh = tv.horizontalHeader()
hh.setStretchLastSection(True)

# set column width to fit contents
tv.resizeColumnsToContents()

# set row height
tv.resizeRowsToContents()

# enable sorting
tv.setSortingEnabled(False)

return tv

def test(self):
self.tabledata.append([1,1,1,1,1])
self.emit(SIGNAL('dataChanged()'))
print 'success'

class MyTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None):
"""
Args:
datain: a list of lists\n
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent)
self.arraydata = datain
self.headerdata = headerdata

def rowCount(self, parent):
return len(self.arraydata)

def columnCount(self, parent):
if len(self.arraydata) > 0:
return len(self.arraydata[0])
return 0

def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])

def setData(self, index, value, role):
pass # not sure what to put here

def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headerdata[col])
return QVariant()

def sort(self, Ncol, order):
"""
Sort table by given column number.
"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.emit(SIGNAL("layoutChanged()"))

if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())

最佳答案

当模型的基础数据发生变化时,模型应该发出 layoutChangedlayoutAboutToBeChanged ,以便 View 正确更新(还有 dataChanged ,如果您想更新特定范围的单元格)。

所以你只需要这样的东西:

    def test(self):
self.tabledata.append([1,1,1,1,1])
self.table.model().layoutChanged.emit()
print 'success'

关于python - PyQt : Adding rows to QTableView using QAbstractTableModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22791760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com