Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QtSql.QSqlRelationalDelegate Validation CloseEditor
QtWS25 Last Chance

[SOLVED] QtSql.QSqlRelationalDelegate Validation CloseEditor

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 928 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeQt
    wrote on last edited by lukeQt
    #1

    Hi Everyone,

    I have subclassed both a QSqlRelationalTableModel and a QtSql.QSqlRelationalDelegate. My question is with validation when editing a cell.

    I have a createEditor method where I specify a line edit and add a regular expression validator to the lineEdit. The user can double click the cell and then click a different cell in the model and then the editor closes.

    This allows the user to not enter anything into the cell and skip the validator. Is this normal behavior? How would I prevent the editor from closing until the regular expression is met?

    Do I need to subclass the method commitAndCloseEditor to get this to work?

    def createEditor(self, parent, option, index):
        """
        This creates the editors and returns the widget used to edit the item
        specified by index for editing.
    
        The parent widget and style option are used to control how the
        editor widget appears.
        """
    
        if index.column() == EVENT_ALIAS:
            lineEdit = QtGui.QLineEdit(parent)
            regex = QtCore.QRegExp(r"[a-zA-Z0-9_]{3,60}")
            validator = QtGui.QRegExpValidator(regex, parent)
            lineEdit.setValidator(validator)
            return lineEdit
    
        # Else return the base method createEditor.
        else:
            return super(Delegate, self).createEditor(parent, option, index)
    
    1 Reply Last reply
    0
    • L Offline
      L Offline
      lukeQt
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • L Offline
        L Offline
        lukeQt
        wrote on last edited by
        #3

        This solution worked for me: http://nullege.com/codes/show/src@p@y@PyQt4-HEAD@examples@tools@settingseditor@settingseditor.py/626/PyQt4.QtGui.QRegExpValidator

        class Delegate(QtSql.QSqlRelationalDelegate):
            """
            Delegate class handles the delegate. This allows for custom editing within
            the GUI. QtSql.QSqlRelationalDelegate is being subclassed to support custom editing.
        
            Methods that are being overridden are:
                createEditor
                setEditorData
                setModelData
            """
        
        
            def __init__(self, parent = None):
                """Class constructor."""
        
                super(Delegate, self).__init__(parent)
        
        
            def createEditor(self, parent, option, index):
                """
                This creates the editors in the delegate, which is reimplemented from
                QAbstractItemDelegate::createEditor(). It returns the widget used to edit
                the item specified by index for editing. The parent widget and style option
                are used to control how the editor widget appears.
                """
        
                column = index.column()
        
                if column == 1:
                    editor = QtGui.QLineEdit(parent)
                    regex = QtCore.QRegExp(r"[a-zA-Z][a-zA-Z0-9_]{3,60}")
                    validator = QtGui.QRegExpValidator(regex,parent)
                    editor.setValidator(validator)
                    return editor
        
                # Else return the base editor. This will handle all other columns.
                else:
                    return super(AppDelegate, self).createEditor(parent, option, index)
        
        
            def setEditorData(self, editor, index):
                """
                 Once the editor has been created and given to the view, the view calls
                 setEditorData(). This gives the delegate the opportunity to populate the
                 editor with the current data, ready for the user to edit.
        
                Sets the contents of the given editor to the data for the item
                at the given index.
        
                Note that the index contains information about the model being used.
        
                The base implementation does nothing.
                If you want custom editing you will need to reimplement this function.
                """
        
                column = index.column()
        
                # Get the data value from the model.
                text = index.model().data(index, QtCore.Qt.DisplayRole).toString()
        
                # Set the editors text to be the text from the model.
                if column == 1:
                    editor.setText(text)
        
                # Else return the base setEditorData method.
                # This is not strictly needed because in flags the ID column is set
                # to be selectable only so it should never call setEditorData.
                else:
                    return super(AppDelegate, self).setEditorData(self, editor, index)
        
        
            def setModelData(self, editor, model, index):
                """
                Sets the data for the item at the given index in the model
                to the contents of the given editor. The base implementation does
                nothing.
                If you want custom editing you will need to reimplement this method.
        
                If the user confirms their edit the editor's data must be written
                back to the model. The model will then notify the views that the item
                has changed, and those views that are showing the item
                will request fresh data to display.
        
                In each case we simply retrieve the value from the appropriate editor,
                and call setData (), passing the values as QVariants.
                """
        
                column = index.column()
        
                # Test if the editor has been modified.
                if not editor.isModified():
                    return
        
                # This ensure that validation passes.
                text = editor.text()
                validator = editor.validator()
                if validator is not None:
                    state, text = validator.validate(text, 0)
                    if state != QtGui.QValidator.Acceptable:
                        return
        
                if column == 1:
                    # Call model.setData and set the data to the text in the QLineEdit.
                    # After the user confirms the edit then set the model data to the
                    # new user input.
                    model.setData(index, QtCore.QVariant(editor.text()))
        
                # else return the base setModelData method.
                else:
                    super(AppDelegate, self).setModelData(self, editor, model, index)
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved