'''
構造函數:
QComboBox(parent=None):創(chuàng)建一個沒有父控件的下拉框。
方法:
addItem(item):向下拉框中添加一個項。
addItems(items):一次性添加多個項。
clear():清除下拉框中的所有項。
itemData(index) 根據index獲取用戶數據
currentIndex():獲取當前選中項的索引。
currentText():獲取當前選中項的文本。
setEditable(isEditable):設置下拉框是否可編輯。
setMaxCount(maxCount):設置下拉框中可顯示的最大項數。
屬性:
count:下拉框中的項數。
信號:
currentIndexChanged(index):當選中的項索引發(fā)生改變時發(fā)射。
currentTextChanged(text):當選中的項的文本發(fā)生改變時發(fā)射(僅在可編輯時有效)
'''
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())
承擔因您的行為而導致的法律責任,
本站有權保留或刪除有爭議評論。
參與本評論即表明您已經閱讀并接受
上述條款。