Skip to content

Instantly share code, notes, and snippets.

@archsh
Created June 21, 2014 01:26
Show Gist options
  • Save archsh/b3b0ceb9d41c21d7b311 to your computer and use it in GitHub Desktop.
Save archsh/b3b0ceb9d41c21d7b311 to your computer and use it in GitHub Desktop.
PyQT 之 QWizardPage registerField注册自定义属性绑定
PyQT 之 QWizardPage registerField注册自定义属性绑定
在使用QWizard的时候,基本上比较多的会使用到registerField的功能,来捕捉控件的属性值并且供全局共享。
Qt也支持自动绑定一些控件的属性,比如:
Widget Property Change Notification Signal
QAbstractButton bool checked toggled()
QAbstractSlider int value valueChanged()
QComboBox int currentIndex currentIndexChanged()
QDateTimeEdit QDateTime dateTime dateTimeChanged()
QLineEdit QString text textChanged()
QListWidget int currentRow currentRowChanged()
QSpinBox int value valueChanged()
但有时候我们想要的属性无法获得,而我们又确实需要通过registerField来传递值,比如ComboBox的itemData,怎么办?
1、重载QComboBox,增加一个属性:
class MyComboBox(QtGui.QComboBox):
"""
Add currentItemData.
"""
@QtCore.pyqtProperty(QtCore.QVariant)
def currentItemData(self):
if self.currentIndex() >= 0:
return self.itemData(self.currentIndex(), QtCore.Qt.UserRole)
else:
return None
2、创建comboBox的时候:
self.comboBox = MyComboBox(self.groupBox)
3、registerField:
self.registerField('myoption', self.comboBox, "currentItemData")
Enjoy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment