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.
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_())