Python Forum
[PyQt] Add validation (regex) to QTableWidget cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Add validation (regex) to QTableWidget cells
#1
Hi all,

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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTableWidget print problem JokerSob 8 4,655 Jan-28-2022, 06:08 PM
Last Post: Axel_Erfurt
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,271 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 5,155 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 3,265 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] QTableWidget stylesheet error mart79 3 6,311 Feb-26-2020, 04:54 PM
Last Post: Denni
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 6,873 Jan-02-2020, 04:32 PM
Last Post: Denni
  [PyQt] QTableWidget cell validation littleGreenDude 1 7,606 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude
  Printing QTableWidget to an DIN A4 page Mady 2 7,137 Dec-18-2018, 06:56 PM
Last Post: Axel_Erfurt
  [PyQt]Find item or text in Qtablewidget maziio 0 11,202 Aug-06-2018, 01:37 PM
Last Post: maziio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020