- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 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_())
最佳答案
当模型的基础数据发生变化时,模型应该发出 layoutChanged或 layoutAboutToBeChanged ,以便 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/
试图更多地了解 QAbstractTableModel,我遇到了 rowCount 和 columnCount 的虚拟方法,这些方法在继承 QAbstractTableModel 时需要实现。 拿 i
出于测试目的,有一个函数要求输入并将其显示在 QListView 中。 display data . 这适用于一个 dim.data 我想扩展脚本显示二维数据 传入的数据应该插入第一行直到第 3 列,
最近又拿起了Qt,开始刷新内存了。为表创建自定义数据模型非常简单。 现在我正在尝试检索选定的数据。请注意,我使用自定义数据对象。 我的自定义模型示例: 平台.h class Platform { pu
如何使 QAbstractTableModel 的数据可检查 我想让下面代码中的每个单元格都可以被用户选中或取消选中,如何修改代码? 根据Qt文档:Qt::CheckStateRole和设置Qt::I
我用通常的方式实现了 QAbstractTableModel: class PrintIntervalTableModel : public QAbstractTableModel { private
我需要用 Qt 实现一个表。 我相信我会起诉一个 QAbstractTableModel,一个 QTableView 使用这个模型。 我知道我必须编辑模型的 rowCount()、columnCoun
我正在尝试显示来自 QAbstractTableModel 的图像。我尝试返回一个QPixmap作为data()的QVariant,但它只产生空单元格,而我期望第二个单元格中的每个单元格列有一个 20
我有一个使用 Q_GLOBAL_STATIC 实现的单例类,它包含一个必须从多个线程访问的数据结构,我在类中实现了访问器函数,它会在访问数据之前锁定互斥量,这样所有对共享数据的访问都是序列化的。 问题
我目前正在尝试制作一个基于QTableView 的vie。因此,我使用 QAbstractTableModel 来存储数据。我的问题是,一旦我从 QVariant headerData(int sec
我有一个子类 class TableModel : public QAbstractTableModel 我重写了 headerData 方法如下: QVariant TableModel::head
我从 QAbstractTableModel 派生了一个模型,现在我想通知一整行的数据已被更改。例如,如果更改了索引为 5 的行的数据(4 列),则使用以下代码将按预期工作。 emit dataCha
我有一个 PyQt QTableView,连接到一个 QAbstractTableModel,它本身连接到一个管理项目列表的自定义类。我可以在列表末尾插入一个项目,它会适本地通知我的模型,然后调用 b
我发现了这个问题:How to change the background color for a QTreeView Header (aka QHeaderView)? 我希望能够为每个标题部分设置
我创建了一个名为 tableModel 的 QAbstractTableModel 模型和一个名为 listModel 的 QAbstractListModel 模型,并将这些模型设置为各自的 Vie
我需要一个能够快速添加和显示大约 100'000 个项目的表格 GUI 控件。出于这个原因,需要“虚拟”表控件,它只加载和显示可见的项目。我尝试了 QTableView 和 QAbstractTabl
QTableView 由QAbstractTableModel 控制。它是填充其内容的模型。模型修改了 self.items 变量,结果是用户双击了 QTableView 的一个项目,然后输入了一个新
这是我在这里的第一篇文章,但自从我开始编程以来,我得到了你们所有人的很多帮助。 我是 Qt 的新手,目前正在尝试制作我的第一个项目。我的问题是关于模型、底层数据和 View 之间的通信。 我首先将后台
在我的应用程序中,我有一个用于保存项目列表的类: class Database : public QObject { Q_OBJECT public: Database(QObject
Qt.AlignRight 右对齐文本但将其放在右上角。 Qt.AlignRight | Qt.AlignVCenter 不起作用。将其放入左上角。 有没有办法让文本同时保持垂直居中和右对齐? 代码示
我有自己的表格模型 (DragDropTableModel),它是 QAbstractTableModel 的子类。在它里面,我希望最后一行始终保持空白。 我通过将 insertRows() 添加到
我是一名优秀的程序员,十分优秀!