python - QDataWidgetMapper and validation -


i have tree view several columns. use qdatawidgetmapper connect each column 1 of several widgets on sidebar. data can changed double-clicking on cell in tree view or using widget on sidebar.

one of columns has string data needs validated. created custom delegate, attached both tree view , data widget mapper. has qregexpvalidator, prevent invalid input while user typing. also, in setmodeldata(), has different validation check once user has pressed "enter". tree view, delegate works fine. mapped qlineedit, there 2 problems:

  1. the qregexpvalidator not called (presumably because createeditor() not used sidebar widget); user can type in bad input qlineedit.
  2. if data fails check during setmodeldata(), text in qlineedit not switch original text. when user clicks on other qlineedit, error message gets printed again.

am going wrong way?

here simplified example. replaced tree view list view simplicity:

class testwidgetmappervalidate(qtgui.qmainwindow):     def __init__(self, parent=none):         super(testwidgetmappervalidate, self).__init__(parent)          self.centralwidget = qtgui.qwidget()         self.setcentralwidget(self.centralwidget)         self.mainlayout = qtgui.qvboxlayout(self.centralwidget)          # set list view         self.listview = qtgui.qlistview()         self.listmodel = qtgui.qstringlistmodel(['aaa', 'bbb', 'ccc', 'ddd'])         self.listview.setmodel(self.listmodel)          # set delegate         self.testdelegate = testdelegate()         self.listview.setitemdelegateforcolumn(0, self.testdelegate)          self.lineedit = qtgui.qlineedit()          self.mainlayout.addwidget(self.listview)         self.mainlayout.addwidget(self.lineedit)          # set qdatawidgetmapper         self.mapper = qtgui.qdatawidgetmapper()         self.mapper.setmodel(self.listmodel)         self.mapper.addmapping(self.lineedit, 0)         self.mapper.setitemdelegate(self.testdelegate)          self.listview.selectionmodel().currentchanged.connect(self.mapper.setcurrentmodelindex)  class testdelegate(qtgui.qstyleditemdelegate):     def __init__(self, parent=none):         super(testdelegate, self).__init__(parent)      def createeditor(self, parentwidget, option, qmodelindex):         editor = qtgui.qlineedit(parentwidget)         nameregex = qtcore.qregexp('[a-za-z][a-za-z0-9_]+')         editor.setvalidator(qtgui.qregexpvalidator(nameregex))         return editor      def seteditordata(self, editor, qmodelindex):         value = qmodelindex.data(qtcore.qt.displayrole)         editor.settext(value)      def setmodeldata(self, editor, model, qmodelindex):         if not editor.hasacceptableinput():             return false          oldvalue = qmodelindex.data(qtcore.qt.displayrole)         newvalue = editor.text()          if oldvalue != newvalue:             if newvalue in model.stringlist():                 print 'that name exists: {0}'.format(newvalue)                 return false             else:                 return model.setdata(qmodelindex, newvalue)         else:             return true 

(note: using pyside , python 2.7)

here came with. don't know if best solution, works case.

  1. i ended adding qregexpvalidator directly line edit, since couldn't read 1 delegate. here updated lines __init__():

    class testwidgetmappervalidate(qtgui.qmainwindow):     def __init__(self, parent=rsui.getmayamainwindow()):         # code omitted here         self.lineedit = qtgui.qlineedit()         nameregex = qtcore.qregexp('[a-za-z][a-za-z0-9_]+')         self.lineedit.setvalidator(qtgui.qregexpvalidator(nameregex)) 
  2. if data fails check in delegate, call seteditordata() original value force old value. prevents error message being printed twice. here updated setmodeldata() testdelegate class:

    def setmodeldata(self, editor, model, qmodelindex):     if not editor.hasacceptableinput():         return false      oldvalue = qmodelindex.data(qtcore.qt.displayrole)     newvalue = editor.text()      if oldvalue != newvalue:         if newvalue in model.stringlist():             # new value not valid.  set data original value.             self.seteditordata(editor, qmodelindex)             print 'that name exists: {0}'.format(newvalue)             return false         else:             return model.setdata(qmodelindex, newvalue)     else:         return true 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -