QComboBox

是一个集按钮和下拉选项于一体的控件,也称做下拉列表框

方法
addItem()

添加一个下拉选项

addItems()

从列表中添加下拉选项

Clear()

删除下拉选项集合中的所有选项

count()

返回下拉选项集合中的数目

currentText()

返回选中选项的文本

itemText(i)

获取索引为i的item的选项文本

currentIndex()

返回选中项的索引

setItemText(int index,text)

改变序列号为index的文本

信号
Activated

当用户选中一个下拉选项时发射该信号

currentIndexChanged

当下拉选项的索引发生改变时发射该信号

highlighted

当选中一个已经选中的下拉选项时,发射该信号

 baudrate_label = QLabel(' 波  特  率  ')
bytesize_label = QLabel(' 数 据 位' )
parity_label = QLabel(' 校 验 位 ')
stopbit_label = QLabel(' 停 止 位 ') self.serialPort_combox = QComboBox()
port_serial = mylib.getPort()
i = 0
for i in range(len(port_serial)):
self.serialPort_combox.addItem(str(port_serial[i]))#一个个添加
self.serialPort_combox.setCurrentIndex(len(port_serial)-1)#设置默认值为最后一个
self.baudrate_combox = QComboBox()
self.baudrate_combox.addItems(myconst.baudrate_list)#直接添加一个list
self.baudrate_combox.setCurrentText(myconst.baudrate_list[4])#设置默认选择
#for item in myconst.baudrate_list:
#baudrate_combox.addItem(str(item)) self.bytesize_combox = QComboBox()
self.bytesize_combox.addItems(myconst.bytesizes)
self.bytesize_combox.setCurrentText(myconst.bytesizes[3])#设置默认选择
self.parity_combox = QComboBox()
self.parity_combox.addItems(myconst.parity_list)
self.stopbit_combox = QComboBox()
self.stopbit_combox.addItems(myconst.stop_bits)
self.stopbit_combox.setCurrentIndex(0)
#将以上控件添加到formlayout布局中
flayout_1.addRow(serialPort_label, self.serialPort_combox)
flayout_1.addRow(baudrate_label, self.baudrate_combox)
flayout_1.addRow(bytesize_label, self.bytesize_combox)
flayout_1.addRow(parity_label, self.parity_combox)
flayout_1.addRow(stopbit_label, self.stopbit_combox)
#将布局添加到paraConf_combox (QGroupBox) 中
self.portSetting_groupbox.setLayout(flayout_1)
 import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import * class ComboxDemo(QWidget):
def __init__(self,parent=None):
super(ComboxDemo, self).__init__(parent)
#设置标题
self.setWindowTitle('ComBox例子')
#设置初始界面大小
self.resize(300,90) #垂直布局
layout=QVBoxLayout()
#创建标签,默认空白
self.btn1=QLabel('') #实例化QComBox对象
self.cb=QComboBox()
#单个添加条目
self.cb.addItem('C')
self.cb.addItem('C++')
self.cb.addItem('Python')
#多个添加条目
self.cb.addItems(['Java','C#','PHP'])
#当下拉索引发生改变时发射信号触发绑定的事件
self.cb.currentIndexChanged.connect(self.selectionchange) #控件添加到布局中,设置布局
layout.addWidget(self.cb)
layout.addWidget(self.btn1)
self.setLayout(layout) def selectionchange(self,i):
#标签用来显示选中的文本
#currentText():返回选中选项的文本
self.btn1.setText(self.cb.currentText())
print('Items in the list are:')
#输出选项集合中每个选项的索引与对应的内容
#count():返回选项集合中的数目
for count in range(self.cb.count()):
print('Item'+str(count)+'='+self.cb.itemText(count))
print('current index',i,'selection changed',self.cb.currentText()) if __name__ == '__main__':
app=QApplication(sys.argv)
comboxDemo=ComboxDemo()
comboxDemo.show()
sys.exit(app.exec_())

Pyqt5_QComboBox的更多相关文章

随机推荐

  1. 美国在线CEO:雅虎被Verizon收购或导致裁员

    北京时间9月13日消息,据外媒报道,AOL首席执行官蒂姆·阿姆斯特朗(Tim Armstrong)称,雅虎.AOL和Verizon整合业务,将导致"部分工作岗位的变化". 阿姆斯特 ...

  2. PPT模板素材

    http://588ku.com/sucai/0-dnum-0-54-0-1/

  3. 如何在Vue项目中优雅的使用sass

    开始之前,请先确保有一个基于webpack模板的项目(vue-cli脚手架一键安装~) 1.打开项目终端,安装sass的依赖包 npm install --save-dev sass-loader / ...

  4. 基于opencv的人脸识别程序

    1. 解析opencv自带人脸识别源码(……/opencv-3.1.0/samples/cpp/facedetect.cpp) @ 操作系统:Ubuntu 15.04 OpenCV版本:3.1.0 # ...

  5. Android APP 性能测试之 GT 工具

    一.介绍: GT(随身调)是 APP 的随身调测平台,它是直接运行在手机上的"集成调测环境"(IDTE, Integrated Debug Environment).利用 GT,仅 ...

  6. Mysql 远程连接错误排查

    1. 测试本地与远程服务器端口能否连通 telnet  远程IP  端口号 telnet 192.168.1.1 3306 2.如果是在aliyun或者aws云服务器上自建数据库 2.1 在安全组里开 ...

  7. python 安装模块之pip install +模块名的换源写法

    1.采用国内源,加速下载模块的速度2.常用pip源(上一篇博客介绍过):– 豆瓣:https://pypi.douban.com/simple– 阿里:https://mirrors.aliyun.c ...

  8. Linux之进程管理工具supervisor

    环境:CentOS 7官方文档:http://supervisord.org/ 安装 # yum install -y epel-release # yum install -y supervisor ...

  9. C语言程序设计实验报告四

    C程序设计实验报告 姓 名:赖瑾 实验地点:家 实验时间:2020年4月9日 实验项目:5.3.1练习2 求数列的前n项和 5.3.2练习2 求水仙花数 5.3.4 十进制转换 5.3.5练习1 百马 ...

  10. Spring官网阅读(三)自动注入

    上篇文章我们已经学习了1.4小结中关于依赖注入跟方法注入的内容.这篇文章我们继续学习这结中的其他内容,顺便解决下我们上篇文章留下来的一个问题-----注入模型. 文章目录 前言: 自动注入: 自动注入 ...