python - Appending a row with widgets and text to a QStandardItemModel
问题描述
I want to append a QWidget
to a QStandardItemModel
in a QTableView
self.table = QTableView()
self.ui.scrollArea.setWidget(self.table) #Not important but I left it in in case it had something to do with this question
self.model = QStandardItemModel()
self.table.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.table.setModel(self.model)
...
@Slot(None)
def OpenFile(self):
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.ExistingFiles)
if dialog.exec():
filenames = dialog.selectedFiles()
for i in range(len(filenames)):
it = QStandardItem()
it.setText(filenames[i].split("/")[-1])
self.samplespinbox = QDoubleSpinBox()
self.formatdropdown = QComboBox()
samplespinboxholder = QStandardItem()
samplespinboxholder.setData(self.samplespinbox)
formatdropdownholder = QStandardItem()
formatdropdownholder.setData(self.samplespinbox)
self.model.appendRow((it ,samplespinboxholder, formatdropdownholder))
But when I try this the rows appear, event the it
gets displayed, but never the Widgets I use in
解决方案
Adding a widget to the data does not show the widget, it only stores it. If you want to display a widget then use the setIndexWidget method:
for filename in filenames:
samplespinbox = QDoubleSpinBox()
formatdropdown = QComboBox()
it1 = QStandardItem(filename.split("/")[-1])
it2 = QStandardItem()
it3 = QStandardItem()
self.model.appendRow((it1 ,it2, it3))
self.table.setIndexWidget(it2.index(), samplespinbox)
self.table.setIndexWidget(it3.index(), formatdropdown)
推荐阅读
- android - 从 uri 获取数据列的值时出现 IllegalArgumentException
- swagger - 使用什么注释来定义 HTTP 请求标头?
- rust - Rust 编译器是否将 String 隐式转换为 &str?
- c++ - 从 file.cpp 映射到 file.o cmake
- revit-api - C# Revit API,如何使用 ExternalCommand 创建简单的墙?
- python - 无法安装 Pyaudio
- shell - 在 awk 中比较 2 个文件
- javascript - 我如何在nodeJS中比较来自mongodb的日期
- python - 在字典列表中查找键值
- c# - 以特定语言格式化值的任何方式