May-05-2020, 12:51 PM
Hi all,
I have a table (see below example) and want to add a
For a standard QLineEdit it is simple but, I can't find any examples how this is done for a
I have a table (see below example) and want to add a
regex validation to each cell in column 0 and 2 of the QTableWidget.For a standard QLineEdit it is simple but, I can't find any examples how this is done for a
QTableWidget.import sys
from PySide2.QtWidgets import QApplication, QVBoxLayout, QDialog, QTableWidget, QHeaderView
from PySide2.QtCore import QRegExp
from PySide2.QtGui import QRegExpValidator
def regex_validator(regex):
regex_expression = QRegExp(regex)
regex_validator = QRegExpValidator(regex_expression)
return regex_validator
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.grid = QTableWidget(self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.grid)
self.setLayout(self.layout)
self.table_settings()
def table_settings(self):
column_size = [100, 150, 100, 100]
column_labels = ["A", "B", "C", "D"]
self.grid.setColumnCount(len(column_size))
self.grid.setRowCount(10)
self.grid.verticalHeader().setVisible(True)
self.grid.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
for cnt, column in enumerate(column_size):
self.grid.setColumnWidth(cnt, column)
row_labels = []
for i in range(10):
row_labels.append(str(i + 1))
self.grid.setHorizontalHeaderLabels(column_labels)
self.grid.setVerticalHeaderLabels(row_labels)
self.grid.setFixedWidth(sum(column_size))
regex = regex_validator(r'(\d+\.{1}\d+|\d+)')
# do something to add the regex to the cells of column 0 and 2?!
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Form()
ex.show()
sys.exit(app.exec_())Your help is much appreciated!
