动态QTableWidget上的交叉列单元格验证

时间:2019-01-18 16:20:33

标签: python python-2.7 pyqt pyqt4 qtablewidget

在动态表上进行单元格验证的推荐方法。

场景:我有一个QDialog,其中基于不同的下拉选择动态地添加了1个或多个表。由于表格是动态添加的,因此标准单元格单击信号是不够的。它仅提供行和列,我需要知道除了行和列之外还单击了哪个表。更具体地说,我有2个具有整数值的列。在其中一个列中更改单元格时,它们必须在有效范围内,并且第二列中的单元格值必须> =第一列中的单元格值。

我对Python还是很陌生,但是我的想法是我需要创建一个类,用我需要的其他信息扩展QTableWidgetItem并发送自定义信号,然后可以将其连接到对话框中的插槽。我已经尝试了以下代码的几种变体,但无法完全解决问题:

class SmartCell(QtCore.QObject):

    valueChanged = QtCore.pyqtSignal(str)  # Signal to be emitted when value changes.

    def __init__(self, tbl, rowname, colname, value):
        QtGui.QTableWidgetItem.__init__(self)
        self.tbl_name = tbl
        self.row_name = rowname
        self.col_name = colname
        # self.setText(value)
        self.__value = value

    @property
    def value(self):
        return self.__value

    @value.setter
    def value(self, value):
        if self.__value != value:
            self.__value = value
            # self.setText(value)
            signal = self.tbl_name + ":" + self.row_name + ":" + self.col_name + ":" + self.text()
            self.valueChanged.emit(signal)

,然后在对话框中,将SmartCell引用导入为sCell:

item = sCell(obj_name, f.part_name, "start_frame", str(f.start_frame))
item.valueChanged.connect(self.frame_cell_changed)
tbl.setItem(rowcounter, 1, item)
item = sCell(obj_name, f.part_name, "end_frame", str(f.end_frame))
item.valueChanged.connect(self.frame_cell_changed)
tbl.setItem(rowcounter, 2, item)

1 个答案:

答案 0 :(得分:2)

您变得太复杂了,任务是验证委托人应该做什么,而不是使用QTableWidgetItems创建验证后逻辑。

import random
from PyQt4 import QtCore, QtGui

class LimistDelegate(QtGui.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = super(LimistDelegate, self).createEditor(parent, option, index)
        if index.column() in (1, 2):
            editor = QtGui.QSpinBox(parent)
        return editor

    def setEditorData(self, editor, index):
        if index.column() in (1, 2):
            m = 0 if index.column() == 1 else index.sibling(index.row(), 1).data()
            M = index.sibling(index.row(), 2).data() if index.column() == 1 else 360
            if hasattr(m, 'toPyObject'):
                m = m.toPyObject()
            if hasattr(M, 'toPyObject'):
                M = M.toPyObject()
            editor.setMinimum(m)
            editor.setMaximum(M)
        super(LimistDelegate, self).setEditorData(editor, index)

def create_table():
    nrows, ncols = random.randrange(3, 6), 3
    table = QtGui.QTableWidget(nrows, ncols)
    for r in range(nrows):
        text = "description {}".format(r)
        a = random.randrange(0, 180) 
        b = random.randrange(a, 360)
        for c, val in enumerate([text, a, b]):
            it = QtGui.QTableWidgetItem()
            it.setData(QtCore.Qt.DisplayRole, val) # set data on item
            table.setItem(r, c, it)
    return table

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        vlay = QtGui.QVBoxLayout(self)
        for _ in range(4):
            table = create_table()
            delegate = LimistDelegate(table) # create delegate
            table.setItemDelegate(delegate)  # set delegate
            vlay.addWidget(table)

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