自定义QAbstractItemDelegate,带有用于QSqlTableModel的正则表达式

时间:2015-01-13 18:50:27

标签: python regex pyqt4 qsqltablemodel

我正在尝试创建自定义委托,以便我可以使用正则表达式来验证输入到表中的数据但由于某种原因,我的代码一直在抛出错误,是否有一个很好的结构化示例?

这是我目前得到的两个错误,当我使用QLineEdit.setText修复AttributeError: 'QLineEdit' object has no attribute 'set'时,我的正则表达式不起作用,它允许添加任何值。

Traceback (most recent call last):
  File "F:\Computing\Program V3\stockGui.py", line 23, in setEditorData
    editor.set(text)
AttributeError: 'QLineEdit' object has no attribute 'set'
Traceback (most recent call last):
  File "F:\Computing\Program V3\stockGui.py", line 29, in setModelData
    model.setData(index, QVariant(editor.text()))
NameError: name 'QVariant' is not defined






class ProductDelegate(QtSql.QSqlRelationalDelegate):
    def __init__(self):
        super().__init__()      

    def createEditor(self, parent, option, index):
        if index.column() == 1:
            editor = QtGui.QLineEdit(parent)
            regex = QtCore.QRegExp(r"(?:[A-Z|\s]+)")
            validator = QtGui.QRegExpValidator(regex,parent)
            editor.setValidator(validator)
            return editor
        else:
            return QtSql.QSqlRelationalDelegate.createEditor(self, parent, option, index)

    def setEditorData(self, editor, index):
        if index.column() == 1:
            text = index.model().data(index, QtCore.Qt.DisplayRole)
            editor.set(text)
        else:
            QtSql.QSqlRelationalDelegate.setEditorData(self, editor,index)

    def setModelData(self, editor, model, index):
        if index.column() == 1:
            model.setData(index, QVariant(editor.text()))
        else:
            QtSql.QSqlRelationalDelegate.setModelData(self, editor, model, index)

2 个答案:

答案 0 :(得分:1)

正如您所发现的,setText没有做任何验证。因此,请使用insert

    text = index.model().data(index, QtCore.Qt.DisplayRole)
    editor.clear()
    editor.insert(text)

导致其他错误的原因仅仅是因为您没有导入QVariant,因此您无法使用它。最简单的解决方法是完全省略它(PyQt将自动转换为QVariant的参数,只要它合适):<​​/ p>

    model.setData(index, editor.text())

(注意:如果您使用的是Python 3,那么默认情况下,QVariant始终会自动转换为等效的Python类型。

答案 1 :(得分:1)

上述解决方案对我不起作用。

此解决方案对我有用:http://nullege.com/codes/show/src@p@y@PyQt4-HEAD@examples@tools@settingseditor@settingseditor.py/626/PyQt4.QtGui.QRegExpValidator

    # 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

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)