Results 1 to 2 of 2

Thread: QtSql.QSqlRelationalDelegate Validation

  1. #1

    Default QtSql.QSqlRelationalDelegate Validation

    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?

    Qt Code:
    1. def createEditor(self, parent, option, index):
    2. """
    3. This creates the editors and returns the widget used to edit the item
    4. specified by index for editing.
    5.  
    6. The parent widget and style option are used to control how the
    7. editor widget appears.
    8. """
    9.  
    10. if index.column() == EVENT_ALIAS:
    11. lineEdit = QtGui.QLineEdit(parent)
    12. regex = QtCore.QRegExp(r"[a-zA-Z0-9_]{3,60}")
    13. validator = QtGui.QRegExpValidator(regex, parent)
    14. lineEdit.setValidator(validator)
    15. return lineEdit
    16.  
    17. # Else return the base method createEditor.
    18. else:
    19. return super(Delegate, self).createEditor(parent, option, index)
    To copy to clipboard, switch view to plain text mode 

  2. #2

    Default Re: QtSql.QSqlRelationalDelegate Validation

    I think I found a workaround.

    In the setModelData I make sure that the length is greater then 3 before calling setData.

    Qt Code:
    1. def setModelData(self, editor, model, index):
    2. """
    3. Gets data from the editor widget and stores it in the specified model
    4. at the item index.
    5.  
    6. The default implementation gets the value to be stored in the data model
    7. from the editor widget's user property.
    8. """
    9.  
    10. # Call the setData method and set the data to the editors text. This
    11. # allows the user to write the edit text back to the model.
    12. if index.column() in [2, 3]:
    13. # Only set the data if the len of the text is greater then 3.
    14. # This means the data will not be set if it is less then 3.
    15. # this will force the validator to be met here.
    16. if len(editor.text()) >3:
    17. model.setData(index, QtCore.QVariant(editor.text()))
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. refreshing QSqlRelationalDelegate data
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2010, 00:54
  2. QSqlRelationalDelegate
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2010, 12:05
  3. Replies: 1
    Last Post: 7th February 2010, 08:01
  4. Replies: 1
    Last Post: 20th January 2010, 22:59
  5. Error in qsqlrelationaldelegate.h?
    By brcain in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2006, 12:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.