简体   繁体   English

在 p 中的 QTablewidget 中插入具有单键多值的字典

[英]Insert dictionary having single key multiple value in QTablewidget in p

单键具有多重值的字典

For one of my automation i need to insert a dictionary with key in 1st column and multiple values attached to key in second column as shown in image above in qtablewidget.On googling i found some code but didnt get how data[row][col] represents dictionary.Need help in correcting this code or better way to write if it is incorrect对于我的自动化之一,我需要在第一列中插入一个字典,并在第二列中的键上附加多个值,如上图 qtablewidget 所示。在谷歌搜索中我找到了一些代码,但没有得到数据 [row] [col]表示字典。如果不正确,需要帮助纠正此代码或更好的编写方式

self.tableWidget.setRowCount(len(dict))
self.tableWidget.setColumnCount(2)
for row in range(len(dict)):
    for col in range(2):
        item = QtWidgets.QTableWidgetItem(data[row][col], 0)
        self.tableWidget.setItem(row, col, item)

The code you found is clearly for a different type of structure, which is probably something like this:您找到的代码显然是针对不同类型的结构的,可能是这样的:

data = {
    0: ['a', 'b'],
    1: ['q', 'w'],
    ...
}

So in the case above accessing data[row][col] means that data[0][0] will be a , data[0][1] will be b , etc.因此,在上述情况下,访问data[row][col]意味着 data[0][0] 将是a , data[0][1] 将是b ,等等。

Besides that, in order to do what you're asking (which is not really related to the code you provided), you need to use setSpan() (it's a method of QTableView, from which QTableWidget is inherited).除此之外,为了做你所要求的(这与你提供的代码并不真正相关),你需要使用setSpan() (它是 QTableView 的一个方法,QTableWidget 继承自该方法)。

In the following example, I'm cycling through dictionary keys, create the first column item and the "sibling" items from the values corresponding to each key, then I use setSpan with the first item row and column, with a row span set to the length of the values, and a column span set to 1.在下面的示例中,我在字典键中循环,从与每个键对应的值创建第一个列项和“兄弟”项,然后我使用setSpan与第一项行和列,行跨度设置为值的长度,以及设置为 1 的列跨度。

Note that I'm using sorted(dict.keys()) for consistency, but if you're using Python >= 3.7 the keys are always sorted in the insertion order.请注意,我使用sorted(dict.keys())以保持一致性,但如果您使用 Python >= 3.7,则键始终按插入顺序排序。

from PyQt5 import QtWidgets

structure = {
    'a.exe': [
        'a', 'b', 'c','d','e','f'
    ], 
    'b.exe': [
        'q', 'n', 'g', 'l', 'm', 'n', 'o'
    ]}

class Test(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QVBoxLayout(self)
        self.table = QtWidgets.QTableWidget()
        layout.addWidget(self.table)

        self.table.setColumnCount(2)
        for key in sorted(structure.keys()):
            values = structure[key]
            row = self.table.rowCount()
            self.table.insertRow(row)
            firstItem = QtWidgets.QTableWidgetItem(key)
            self.table.setItem(row, 0, firstItem)

            for i, value in enumerate(values):
                if i:
                    # add rows only from the second item
                    self.table.insertRow(row + i)
                item = QtWidgets.QTableWidgetItem(value)
                self.table.setItem(row + i, 1, item)

            # set the table span for the "key" item
            self.table.setSpan(firstItem.row(), 0, len(values), 1)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Test()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM