Answer the question
In order to leave comments, you need to log in
Python. The Validator does not allow you to erase the value of the TableWidget. Has anyone experienced this?
Hello! If anyone has come across or knows how to solve it, please tell me ... I
drew a tableWidget, hung validators (double, int).
When I enter a value into a cell, the validator works, everything is ok. But when I want to clear a previously entered cell, the value is not erased.
Can you please tell me how to get around this problem?
My example:
# -*- 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_())
Answer the question
In order to leave comments, you need to log in
In general, in Qt, if you don’t like something, you need to inherit and rewrite the method, changing the behavior to the desired one - this is the standard way in Qt, unfortunately.
Like this:
class MyDoubleValidator(QtGui.QDoubleValidator):
def __init__(self, *args):
super(MyDoubleValidator, self).__init__(*args)
def validate(self, string, number):
if not string:
return (QtGui.QValidator.Acceptable, string, number)
return QtGui.QDoubleValidator().validate(string, number)
validator = MyDoubleValidator()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question