Всем, привет! Если кто сталкивался или знает как решить, подскажите пожалуйста ...
Нарисовал таблицу tableWidget, навесил валидаторы(double, int).
Когда ввожу значение в ячеку - валидатор работает, все ок. Но когда хочу почистить ранее введенную ячейку, значение не стирается.
Подскажите пожалуйста, как можно обойти данную проблему?
Мой пример:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QDialog, QVBoxLayout
from PyQt5.Qt import QLabel, QDialogButtonBox, Qt
class ValidatedItemDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent = None):
super(ValidatedItemDelegate, self).__init__(parent)
def createEditor(self, parent, option, index):
if not index.isValid():
return 0
if index.column() == 0 or index.column() == 2:
editor = QtWidgets.QLineEdit(parent)
validator = QtGui.QDoubleValidator()
editor.setValidator(validator)
return editor
if index.column() == 1:
editor = QtWidgets.QLineEdit(parent)
#validator = QtGui.QDoubleValidator()
validator = QtGui.QIntValidator()
editor.setValidator(validator)
return editor
return super(ValidatedItemDelegate, self).createEditor(parent, option, index)
class Ui_Primer(QtWidgets.QWidget):
def setupUi(self, Primer):
Primer.setObjectName("Primer")
Primer.setFixedSize(290, 245)
self.groupBox = QtWidgets.QGroupBox(Primer)
self.groupBox.setGeometry(QtCore.QRect(15, 15, 260, 215))
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.tableWidget = QtWidgets.QTableWidget(self.groupBox)
self.tableWidget.setGeometry(QtCore.QRect(20, 20, 222, 175))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(6)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 0, item)
i = i + 1
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 1, item)
i = i + 1
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 2, item)
i = i + 1
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 3, item)
i = i + 1
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 4, item)
i = i + 1
i = 0
while i <= 6:
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(i, 5, item)
i = i + 1
#start - Вешаем валидатор
self.tableWidget.setItemDelegate(ValidatedItemDelegate())
#end - Вешаем валидатор
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.horizontalHeader().setDefaultSectionSize(110)
self.tableWidget.horizontalHeader().setHighlightSections(False)
self.tableWidget.horizontalHeader().setMinimumSectionSize(25)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.verticalHeader().setDefaultSectionSize(25)
self.tableWidget.verticalHeader().setMinimumSectionSize(25)
self.retranslateUi(Primer)
QtCore.QMetaObject.connectSlotsByName(Primer)
def retranslateUi(self, Primer):
_translate = QtCore.QCoreApplication.translate
Primer.setWindowTitle(_translate("Primer", "Пример"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("Primer", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("Primer", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("Primer", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("Primer", "4"))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("Primer", "6"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Primer", "-- Double --"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Primer", "-- Int --"))
item = self.tableWidget.horizontalHeaderItem(2)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Primer = QtWidgets.QWidget()
ui = Ui_Primer()
ui.setupUi(Primer)
Primer.show()
sys.exit(app.exec_())