0
代碼工作在API第1罰款,如果我去掉頂部SIP呼叫,那麼就意味着我將使用V2,那麼我當然會看到像如何在api v2中重新實現qvalidator.validate()?
TypeError: invalid result type from Validator.validate()
錯誤,我知道我需要以改變.validate()的返回值,但是如果我將其更改爲第2版中的pyqt文檔所述,它會給我提供分段錯誤(在pyqt 4.6.1,centos 6.3 x64上)。爲什麼?
v2代碼工作正常pyqt 4.8和4.10雖然(在Fedora 19 Linux和Windows 7機器上測試)。這是一個錯誤,或者我錯過了什麼?
的感謝!
#!/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_())