该代码在 api v1 中运行良好,如果我取消注释顶部的 sip 调用,那么这意味着我将使用 v2,那么我当然会看到类似的错误
TypeError: invalid result type from Validator.validate()
我知道我需要更改 .validate() 的返回值,但如果我将其更改为 pyqt doc 在 v2 中所说的内容, http://pyqt.sourceforge.net/Docs/PyQt4/qvalidator.html#validate-2 ,它给了我分段错误(在 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_())