PyQt5 QComboBox控件 槽函数里获取选择的对应的object,Qt.UserRole的使用

class myMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
		self.pen = QPen(Qt.black, 2, Qt.SolidLine)
		self.penStyleComboBox = QComboBox()
		self.penStyleComboBox.addItem("Solid",Qt.SolidLine)
		self.penStyleComboBox.addItem('Dash',  Qt.DashLine)
		self.penStyleComboBox.addItem('Dot',  Qt.DotLine)
		self.penStyleComboBox.addItem('Dash Dot',  Qt.DashDotLine)
		self.penStyleComboBox.addItem('Dash Dot Dot',  Qt.DashDotDotLine)
		self.penStyleComboBox.addItem('None',  Qt.NoPen)
        #....省略控件的布局,信号触发等
	def xxx(self):#槽函数
		style = Qt.PenStyle(self.penStyleComboBox.itemData(self.penStyleComboBox.currentIndex(),Qt.UserRole))
		self.pen.setStyle(style)
  • 这种方法无法用.addItems(),要一个个去add。
  • 常规方法是 用.addItems()在槽函数里用if elif …来判断self.penStyleComboBox.currentText()去确定style。

你可能感兴趣的:(PyQt5,python)