获取知道其元素的QTableWidget行的索引 [英] Get the index of a QTableWidget row knowing its elements
问题描述
[我正在使用PyQt4,但是我认为这个Qt4问题不是Python特有的.]
[I'm using PyQt4, but I think this Qt4 issue is not Python specific.]
我有一个QTableWidget
.在每一行中,第一列包含一个按钮.单击后,该行将被删除.
I have a QTableWidget
. In each row, the first column holds a button. When clicked, the row is removed.
要删除该行,我使用removeRow(int row)
方法,该方法将行的索引作为参数.连接信号时,我不知道行的索引,因为它同时可能会发生变化(例如,如果删除了第一行,则所有行的索引都会更改).
To remove the row, I use removeRow(int row)
method, which takes as argument the index of the row. When connecting the signal, I can't know the index of the row because it might change in the meantime (for instance if the first row is removed, all row indexes are changed).
此处接受的答案建议在行中向回调函数传递QTableWidgetItem的实例,然后在删除时从该项目获取行号.
The accepted answer here suggests to pass the callback an instance of a QTableWidgetItem in the line, then get the row number from this item at deletion time.
这很好,除了该行的所有元素都不是QTableWidgetItem
.元素是按钮本身和一些ComboBox.
This would be nice, except none of the elements of the row is a QTableWidgetItem
. The elements are the button itself and a few ComboBoxes.
我想不出办法解决这个问题.
I can't figure out a way around this.
我可以以某种方式使我的元素之一适合QTableWidgetItem吗?我应该在某种隐藏列中添加QTableWidgetItem吗?
Can I somehow fit one of my elements into a QTableWidgetItem? Should I add a QTableWidgetItem in some sort of hidden column?
我们当前的实现使用indexAt(QtGui.qApp.focusWidget())
(请参阅上面提到的问题的其他答案),这对我来说似乎是一个遗憾的解决方法.
Our current implementation uses indexAt(QtGui.qApp.focusWidget())
(see other answer to question mentioned above), which looks like a sorry workaround to me.
如果我用这样的可检查QTableWidgetItem替换按钮
If I replace the button with a checkable QTableWidgetItem like this
rm_item = QtGui.QTableWidgetItem()
rm_item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsEnabled)
我有一个QTableWidgetItem,可以用来返回到行索引.但是我不知道如何像使用按钮一样从中捕获选中"或单击"事件.我发现的只是QTableWidget
的itemClicked
信号,但是随后我不得不过滤掉所有其他小部件.
I have a QTableWidgetItem I can use to get back to the row index. But I don't know how to catch a "checked" or "clicked" event from it like I do with the button. All I found is the itemClicked
signal of QTableWidget
, but then I'd have to filter all the other widgets out.
我必须缺少一些明显的东西.
There has to be something obvious I'm missing.
修改
根据我在此处所读的内容,我可以添加带有setItem的QTableWidgetItem和带有setCellWidget的Button窗口部件都位于同一单元格中.在我看来,这似乎并不自然,但显然可以(目前无法测试).
From what I read here, I could add both a QTableWidgetItem with setItem and a Button widget with setCellWidget to the same cell. This doesn't seem so natural to me, but apparently it works (can't test right now).
我想我会做的.添加按钮,并在同一单元格上添加一个伪QTableWidgetItem,以作为对该行的引用.
I guess I'll do that. Add the Button, plus a dummy QTableWidgetItem on the same cell to pass as a reference to the row.
这就是它的本意吗?
编辑2
或者也许QTableWidget
不是正确的Widget,我应该使用Layout
,如建议的此处.
Or maybe QTableWidget
is not the proper Widget and I should be using a Layout
, as suggested here.
推荐答案
看来,使用布局而不是表格可能是最正确"的答案,但正如我在答案中看到的那样,这可能会带来自身的困难.这个问题:
It seems that using a layout rather than a table is possibly the most "correct" answer, but that may come with it's own difficulties, as seen in my answer to this question:
如果您要继续使用表,比添加虚拟项目更干净的解决方案是使用永久模型索引:
If you want to continue using a table, a somewhat cleaner solution than adding dummy items would be to use a persistent model index:
button = QtGui.QPushButton(text, self.table)
self.table.setCellWidget(row, column, button)
index = QtCore.QPersistentModelIndex(
self.table.model().index(row, column))
button.clicked.connect(
lambda *args, index=index: self.handleButton(index))
def handleButton(self, index):
print('button clicked:', index.row())
if index.isValid():
self.table.removeRow(index.row())
这篇关于获取知道其元素的QTableWidget行的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!