构造函数:
QComboBox(parent=None):创建一个没有父控件的下拉框。
方法:
addItem(item):向下拉框中添加一个项。
addItems(items):一次性添加多个项。
clear():清除下拉框中的所有项。
itemData(index) 根据index获取用户数据
currentIndex():获取当前选中项的索引。
currentText():获取当前选中项的文本。
setEditable(isEditable):设置下拉框是否可编辑。
setMaxCount(maxCount):设置下拉框中可显示的最大项数。
属性:
count:下拉框中的项数。
信号:
currentIndexChanged(index):当选中的项索引发生改变时发射。
currentTextChanged(text):当选中的项的文本发生改变时发射(仅在可编辑时有效)
'''
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.resize(400,400)
self.setWindowTitle("QComboBox下拉组合框")
self.cb = QComboBox(self)
self.cb.setGeometry(30,30,100,30)
self.cb.addItem("S7",102)
self.cb.addItem("Modbus Tcp",502)
self.cb.addItem("Modbus RTU",502)
self.cb.addItem("OPU UA",4884)
self.cb.addItem("TCP",8989)
# self.cb.addItems(["S7","Modbus Tcp","Modbus RTU","OPU UA","TCP"])
self.cb.setCurrentIndex(2)
# self.cb.clear()
# print(self.cb.currentIndex(),self.cb.currentText())
# print(self.cb.count())
# self.cb.setEditable(True)
# self.cb.setMaxCount(3)
self.cb.currentIndexChanged.connect(self.on_cb_index_changed)
self.on_cb_index_changed(self.cb.currentIndex())
# self.cb.currentTextChanged.connect(self.on_cb_text_changed)
# self.on_cb_text_changed(self.cb.currentText())
def on_cb_index_changed(self,index):
# print(index)
print(self.cb.itemData(index))
def on_cb_text_changed(self,cb_name):
print(cb_name)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())


承担因您的行为而导致的法律责任,
本站有权保留或删除有争议评论。
参与本评论即表明您已经阅读并接受
上述条款。