0

The code works fine in api v1, if i uncomment the top sip calls, then it means i'll be using v2, then of course i'll see errors like

TypeError: invalid result type from Validator.validate()

i know i need to change the return value of .validate() , but if i change it to what pyqt doc says in v2 , http://pyqt.sourceforge.net/Docs/PyQt4/qvalidator.html#validate-2 ,it gives me segmentation fault ( on pyqt 4.6.1, centos 6.3 x64). why?

v2 code works fine on pyqt 4.8 and 4.10 though ( tested on a fedora 19 linux and a windows 7 machine.) is this a bug or what am i missing ?

thanks !

#!/usr/bin/env python2

# api v2
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import re,os,sys

class MyQComboBox(QComboBox):
    def __init__(self, parent=None, listing=None):
        super(MyQComboBox, self).__init__(parent)

        if listing != None:
            self.listSetup(listing)

    def listSetup(self, listing):
        self.setEditable(True)

        self.listing = listing
        self.clear()
        self.addItems(self.listing)

        self.completer = QCompleter(self.listing)
        self.setCompleter(self.completer)

        self.valid = QValidator_listValid(self, listing = self.listing)  
        self.setValidator(self.valid)


# api v1
# class QValidator_listValid(QValidator):
#     def __init__(self, parent=None, listing=None):
#         super(QValidator_listValid, self).__init__(parent)
#         self.listing = listing

#     def validate(self, input, pos=None):
#         for name in self.listing:
#             if re.match(str(input), name, re.I):
#                 return (QValidator.Acceptable, pos)
#         return (QValidator.Invalid, pos)

# api v2
class QValidator_listValid(QValidator):
    def __init__(self, parent=None, listing=None):
        super(QValidator_listValid, self).__init__(parent)
        self.listing = listing

    def validate(self, input, pos=None):
        for name in self.listing:
            if re.match(input, name, re.I):
                return (QValidator.Acceptable,input, pos)
        return (QValidator.Invalid,input,pos)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    ui = MyQComboBox()
    ui.listSetup(['aaa','bbb','ccc'])
    ui.show()
    sys.exit(app.exec_())
Shuman
  • 3,914
  • 8
  • 42
  • 65

1 Answers1

0

I am not expert but maybe this help:

class MetaNameLineEdit(QtGui.QLineEdit):
    Prefix = 'New_'
    Pattern = '^{prefix}([A-Za-z0-9_]{{0,{n}}})$'.format(prefix=Prefix, n=50)

    def __init__(self, parent):
        QtGui.QLineEdit.__init__(self, parent)
        self.setValidator(QtGui.QRegExpValidator(QtCore.QRegExp(self.Pattern), self))
toro2k
  • 19,020
  • 7
  • 64
  • 71