如何获取QTableWidget光标下的行 [英] How to get the row under the cursor for a QTableWidget

查看:84
本文介绍了如何获取QTableWidget光标下的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取鼠标指针下的行号?我想在不知道行号的情况下删除一行,但只删除光标位置.

Is there a way to get the row number under the mouse pointer? I want to remove a row without knowing the row number, but with just the cursor position.

在此图像中,您可以为资产库添加项目.单击 Delete Bin 时,应删除该行.最好的方法是在单击 bin 时查询鼠标指针下的 ROW 编号.其中行号被解析为 removeRow 函数.我不知道如何使用 QPointer.并且 cellEntered 需要 row/col 值,这些值在添加或删除新行时不会保持不变.

In This image you can add projects for the Asset Library. When the Delete Bin is clicked the row should be removed. The best way would be to query the ROW number under the Mouse pointer while the bin is clicked. Where the row number is parsed to the removeRow function. I dont know how to make use of the QPointer for that. And cellEntered needs row/col values which wont stay the same when new rows are added or remvoved.

推荐答案

有很多方法可以解决这个问题.一些可能涉及光标位置,而另一些可能涉及表事件和信号.使用 QCursor 解决这个问题的一个问题是,如果有人用键盘而不是鼠标点击来触发按钮,这意味着光标位置可能无法反映正确的行.

There are a number of approaches that could solve this problem. Some can involve cursor location, and others can get into the table events and signals. A problem with using the QCursor to solve this, is if someone triggers the button with the keyboard as opposed to the mouse click, which means the cursor position might not reflect the correct row.

既然您已经在使用高级QTableWidget,那么这里有一个非常简单的方法:

Since you are already using the high-level QTableWidget, then here is a really simple way to do it:

from functools import partial

class Table(QtGui.QWidget):

    def __init__(self):
        super(Table, self).__init__()

        self.table = QtGui.QTableWidget(3, 2, self)

        for row in xrange(3):
            item = QtGui.QTableWidgetItem("Item %d" % row)
            self.table.setItem(row, 0, item)

            button = QtGui.QPushButton("X", self.table)
            self.table.setCellWidget(row, 1, button)

            button.clicked.connect(partial(self._buttonItemClicked, item))

        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.table)

    def _buttonItemClicked(self, item):
        print "Button clicked at row", item.row()

在这个例子中,我们只是将第一列的 item 烘焙到 clicked 回调中,所以当你点击它们时,它们有一个询问行号的参考.对于较低级别的模型/视图,该方法会有所不同.

In this example, we just bake the item of the first column into the clicked callback, so when you click them they have a reference for asking the row number. The approach would be different for lower level model/view.

这篇关于如何获取QTableWidget光标下的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆