我想在PyQt表中显示一个pandas数据帧.我已经取得了一些进展,但是无法正确派生Table Model类.任何有关这方面的帮助将非常感激.
**注意这里的完整示例代码**
我正在努力生成一个有效的QtCore.QAbstractTableModel派生类.继上一个关于QItemDelegates的问题后,我试图从Pandas DataFrame生成一个表模型来插入实际数据.我在这里有一些示例代码,但如果我在Widget类(ln 152)中用TableModel2替换我的TableModel,我就无法显示该表.
class TableModel2(QtCore.QAbstractTableModel):
def __init__(self, parent=None, *args):
super(TableModel2, self).__init__()
#QtCore.QAbstractTableModel.__init__(self, parent, *args)
self.datatable = None
self.headerdata = None
self.dataFrame = None
self.model = QtGui.QStandardItemModel(self)
def update(self, dataIn):
print 'Updating Model'
self.datatable = dataIn
print 'Datatable : {0}'.format(self.datatable)
headers = dataIn.columns.values
header_items = [
str(field)
for field in headers
]
self.headerdata = header_items
print 'Headers'
print self.headerdata
for i in range(len(dataIn.index.values)):
for j in range(len(dataIn.columns.values)):
#self.datatable.setItem(i,j,QtGui.QTableWidgetItem(str(df.iget_value(i, j))))
self.model.setItem(i,j,QtGui.QStandardItem(str(dataIn.iget_value(i, j))))
def rowCount(self, parent=QtCore.QModelIndex()): …Run Code Online (Sandbox Code Playgroud) 我正在从Pandas数据帧动态创建QTableView.我这里有示例代码.
我可以使用复选框创建表格,但是我无法获得反映模型数据的复选框,甚至根本无法更改为取消选中.
我正在关注上一个问题的示例代码,并以@raorao答案为指导.这将显示表格中的框,但不起作用.
任何人都可以建议任何更改,或者此代码有什么问题.为什么它不反映模型,为什么它不会改变?
请查看我的完整示例代码.
编辑一:Frodon评论后更新:更正了字符串强制转换为bool并进行比较xxx =='True'
class CheckBoxDelegate(QtGui.QStyledItemDelegate):
"""
A delegate that places a fully functioning QCheckBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
'''
Important, otherwise an editor is created if the user clicks in this cell.
** Need to hook up a signal to the model
'''
return None
def paint(self, painter, option, index):
'''
Paint …Run Code Online (Sandbox Code Playgroud) 背景:我找不到一个完整的组合框内的例子QTableView.所以我根据其他几个更人为的例子编写了这段代码.但问题是,此示例要求您在组合框启用之前双击组合框,然后您必须再次单击以将其删除.它不是非常用户友好.如果我使用非模型/视图的东西QTableWidget,组合框会在第一次点击时下降.
问题:有人可以看看这个并告诉我需要做些什么才能让它做出响应QTableWidget?此外,如果有任何我正在做的事情是不必要的,请同时说明.例如,是否绝对有必要参考应用程序样式?
import sys
from PyQt4 import QtGui, QtCore
rows = "ABCD"
choices = ['apple', 'orange', 'banana']
class Delegate(QtGui.QItemDelegate):
def __init__(self, owner, items):
super(Delegate, self).__init__(owner)
self.items = items
def createEditor(self, parent, option, index):
self.editor = QtGui.QComboBox(parent)
self.editor.addItems(self.items)
return self.editor
def paint(self, painter, option, index):
value = index.data(QtCore.Qt.DisplayRole).toString()
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionComboBox()
opt.text = str(value)
opt.rect = option.rect
style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter)
QtGui.QItemDelegate.paint(self, painter, option, index)
def setEditorData(self, editor, index):
value = …Run Code Online (Sandbox Code Playgroud) 我是PyQt的新手,我正在开发一个包含QTableView的项目,其中一个列显示系统路径.我想添加一个QTreeView,以便用户可以单击+或>按钮来展开路径下面的内容.
这是我的基本实现:
from PyQt4 import QtGui
from PyQt4 import QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(600,400)
self.setWindowTitle("My Basic Treeview")
self.treeview = QtGui.QTreeView(self)
self.treeview.model = QtGui.QFileSystemModel()
self.treeview.model.setRootPath('/opt')
self.treeview.setModel(self.treeview.model)
self.treeview.setColumnWidth(0, 200)
self.setCentralWidget(self.treeview)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
虽然,在上面的例子中,我得到所有文件夹,但我只想要/opt路径及其下面的文件夹.
import operator
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyWindow(QWidget):
def __init__(self, data_list, header, *args):
QWidget.__init__(self, *args)
# setGeometry(x_pos, y_pos, width, height)
self.setGeometry(300, 200, 570, …Run Code Online (Sandbox Code Playgroud)