PyQt5:获取单元格小部件的索引及其选择的值

PyQt5: get index of cell widgets and their chosen values

我正在使用 Python 3.5,我有一个简单的代码可以创建一个 table,里面有几个组合框。这是代码:

from PyQt5 import QtCore
from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

########################## Create table ##############################
tableWidget = QtWidgets.QTableWidget()
tableWidget.setGeometry(QtCore.QRect(200, 200, 250, 300))
tableWidget.setColumnCount(2)
tableWidget.setRowCount(9)
tableWidget.show()

################# Create tablecontent & comboboxes ###################
names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
i = 0
for j in names:
    tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))

    comboBox = QtWidgets.QComboBox()
    combo_option_list = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]
    for t in combo_option_list:
        comboBox.addItem(t)

    tableWidget.setCellWidget(i, 1, comboBox)
    i += 1

sys.exit(app.exec_()) 

我想使用这些组合框,但有几个问题。它已经开始于我只能通过以下命令使用最后创建的组合框:

comboBox.activated[str].connect(do_something)

我需要了解以下内容:

代码下方说明

from PyQt5 import QtCore
from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

########################## Create table ##############################

tableWidget = QtWidgets.QTableWidget()
tableWidget.setGeometry(QtCore.QRect(200, 200, 250, 300))
tableWidget.setColumnCount(2)
tableWidget.setRowCount(9)
tableWidget.show()

################# Create tablecontent & comboboxes ###################

def dosomething(index):
    widget = app.sender()
    print('widget object:', widget)
    print('widget myname:', widget.my_name)
    print('widget index:', combo_list.index(widget))
    print('option index:', index)

#---------------------------------------------------------------------

names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
combo_option_list = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]

combo_list = []

for i, name in enumerate(names):

    tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(name))

    combobox = QtWidgets.QComboBox()
    combobox.addItems(combo_option_list)
    combobox.my_name = name + ' (i=' + str(i) + ')'
    combobox.currentIndexChanged.connect(dosomething)

    combo_list.append(combobox)

    tableWidget.setCellWidget(i, 1, combobox)

#---------------------------------------------------------------------

sys.exit(app.exec_())

dosomething 中,您可以使用 app.sender() 获取被点击的小部件 - 然后您可以使用它对该小部件执行某些操作。

PyQt 使用参数执行 dosomething - 它是组合框中所选选项的索引。

要在 combo_list 中获取 widget 的索引,您可以使用标准列表方法 combo_list.index(widget)

但您也可以在组合框中创建自己的变量(即 my_name)以保留索引或其他值

combobox.my_name = some_value

以后可以进入dosomething

value = widget.my_name

顺便说一句:

而不是 addItem(one_option)for 循环你可以使用 addItems(option_list)

您可以使用 enumerate(names) 而不需要 i = 0i += 1

for i, j in enumerate(names):

您可以在 for 循环之前创建 combo_option_list

这里棘手的部分是获取组合框在 table 中的索引,因为单元格小部件无法知道自己的索引。解决这个问题的一种方法是使用 table 的 indexAt() 方法,它可以从 QPoint 中获取索引。一个 cell-widget 确实 知道它自己相对于其父元素的位置,因此可以将其传递给 indexAt() 以获取其在 table.[=14 中的索引=]

下面是一个简单的演示,展示了如何连接信号并获取所需的所有信息:

from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.table = QtWidgets.QTableWidget(9, 2, self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.table)
        names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
        options = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]
        for index, name in enumerate(names):
            self.table.setItem(index, 0, QtWidgets.QTableWidgetItem(name))
            comboBox = QtWidgets.QComboBox()
            comboBox.activated.connect(self.comboActivated)
            comboBox.addItems(options)
            self.table.setCellWidget(index, 1, comboBox)

    def comboActivated(self, option):
        combo = self.sender()
        print('option: %s, "%s"' % (option, combo.itemText(option)))
        index = self.table.indexAt(combo.pos())
        print('row: %s, column: %s' % (index.row(), index.column()))

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 200, 300, 300)
    window.show()
    sys.exit(app.exec_())