bril10
bril10

Reputation: 7

Validate text in QTableWidget

I need to validate if a QTableWidget has lost focus, so I can validate the entry text and change its text if it's not valid for my program.

Upvotes: 0

Views: 1407

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

A possible solution is to use a delegate, and the delegate is in charge of validation, in this case use a QLineEdit with an inputMask:

class HexDelegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        w = QLineEdit(parent)
        w.setInputMask("HH")
        return w

class App(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.setLayout(QVBoxLayout())

       # Create table
        self.tableWidget = QTableWidget(self)
        self.layout().addWidget(self.tableWidget)
        self.tableWidget.setRowCount(4)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setItemDelegate(HexDelegate())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions