【发布时间】:2018-01-16 12:02:25
【问题描述】:
我正在尝试使我的 QComboBox 可检查。我搜索了类似的帖子,但其中许多帖子仍未得到答复,并且我没有设法应用此处发布的解决方案 (QCombobox of Checkboxes)。目前我已经定义了这个类:
class myCombo(QComboBox):
def __init__(self, parent = None):
super(myCombo, self).__init__(parent)
self.setStyleSheet("QComboBox{"
"font-size:12px;"
"color:black;"
"background-color:white;"
"border:1px solid black;"
"padding:1px;""}")
self.setEditable(True)
self.lineEdit().setAlignment(Qt.AlignCenter)
self.lineEdit().setReadOnly(True)
我的目标是添加一个能够检查项目的属性,以便可以做出多个选择。是否可以在不将此“复选框”硬编码到 QComboBox 的情况下对其进行管理?
上面定义的 QComboBox 类是这样使用的:
class sWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
years = [' ', '2017', '2018', '2019', '2020']
months = [' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
self.setMinimumSize(QSize(360, 140))
self.setWindowTitle("Dialog")
self.yearLabel = QLabel(self)
self.yearLabel.setText('Select Year(s):')
self.firstLine = myCombo(self)
self.firstLine.addItems(years)
self.firstLine.move(120, 20)
self.firstLine.resize(200, 32)
self.yearLabel.move(20, 20)
self.monthLabel = QLabel(self)
self.monthLabel.setText('Select Month(s):')
self.secondLine = myCombo(self)
self.secondLine.addItems(months)
self.secondLine.move(120, 60)
self.secondLine.resize(200, 32)
self.monthLabel.move(20, 60)
okButton = QPushButton('OK', self)
okButton.clicked.connect(self.okClicked)
okButton.resize(100, 32)
okButton.move(120, 100)
cancelB = QPushButton('Cancel', self)
cancelB.clicked.connect(self.cancelClicked)
cancelB.resize(100, 32)
cancelB.move(220, 100)
def okClicked(self):
self.close()
def cancelClicked(self):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = sWindow()
main.show()
sys.exit(app.exec_())
例如,我希望可以在月份组合框中选择 1、2、3。感谢您的任何建议!
【问题讨论】:
-
这不是一个重复的问题——链接问题中的答案仅适用于 PyQt4,并且此问题明确询问 PyQt5
标签: python python-3.x pyqt5 qcombobox