QTableWidget 当前选择更改信号 [英] QTableWidget Current Selection Change Signal

查看:140
本文介绍了QTableWidget 当前选择更改信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前选择更改时 QTableWidget 的信号是什么,我如何为其分配插槽?

What's the signal of a QTableWidget when the current selection changes and how can I assign a slot to it?

推荐答案

您想要 itemSelectionChanged 信号:

You want the itemSelectionChanged signal:

This signal is emitted whenever the selection changes.

This signal is emitted whenever the selection changes.

您可以通过执行以下操作来使用它:

You can use this by doing something like this:

self.itemSelectionChanged.connect(self.print_row)

这将在每次选择更改时调用 self.print_row(这是您创建的函数).

This will call self.print_row (which is a function you create) every time the selection changes.

一个非常基本的例子:

import sys
from PyQt4 import QtGui, QtCore

lista = ['r1c1', 'r1c2', 'r1c3']
listb = ['r2c1', 'r2c2', 'r1c3']
listc = ['r3c1', 'r3c2', 'r3c3']
mystruct = {'row1':lista, 'row2':listb, 'row3':listc}

class MyTable(QtGui.QTableWidget):
    def __init__(self, thestruct, *args):
        QtGui.QTableWidget.__init__(self, *args)
        self.data = thestruct
        n = 0
        for key in self.data:
            m = 0
            for item in self.data[key]:
                newitem = QtGui.QTableWidgetItem(item)
                self.setItem(m, n, newitem)
                m += 1
            n += 1
        self.itemSelectionChanged.connect(self.print_row)

    def print_row(self):
        items = self.selectedItems()
        print(str(items[0].text()))

def main(args):
    app = QtGui.QApplication(args)
    table = MyTable(mystruct, 3, 3)
    table.show()

    sys.exit(app.exec_())

if __name__=="__main__":
    main(sys.argv)

<小时>

这会创建一个这样的表:


This creates a table like this:

当您选择任何单元格时,它会打印出单元格中的文本.请注意,此打印功能假定您一次只能进行一个选择.selectedItems() 函数返回所选项目的列表.在这个例子中我只使用了第一个索引.

When you select any of the cells, it will print out the text in the cell. As a note, this print function assumes that you only make one selection at a time. The selectedItems() function returns a list of selected items. I am only using the first index in this example.

这篇关于QTableWidget 当前选择更改信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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