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. Mybatis详解系列(五)--Mybatis Generator和全注解风格的MyBatis3DynamicSql

    简介 Mybatis Generator (MBG) 是 Mybatis 官方提供的代码生成器,通过它可以在项目中自动生成简单的 CRUD 方法,甚至"无所不能"的高级条件查询(M ...

  2. STL 训练 POJ - 1862 Stripies

    Description Our chemical biologists have invented a new very useful form of life called stripies (in ...

  3. P1465 序言页码 Preface Numbering (手推)

    题目描述 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 最多3个同样的可以表示为 ...

  4. 过滤idea一些不需要的文件和文件夹的显示,在使用svn的时候可以很方便的过滤不需要提交的文件

    *.classpath;*.gitignore;*.hprof;*.idea;*.iml;*.lst;*.project;*.pyc;*.pyo;*.rbc;*.settings;*.sh;*.yar ...

  5. 【FPGA篇章三】FPGA常用语句:Verilog基本语法要素

    欢迎大家关注我的微信公众账号,支持程序媛写出更多优秀的文章 Verilog中总共有十九种数据类型,我们先介绍四个最基本的数据类型,他们是: reg型.wire型.integer型.parameter型 ...

  6. 【Spark】SparkStreaming从不同基本数据源读取数据

    文章目录 基本数据源 文件数据源 注意事项 步骤 一.创建maven工程并导包 二.在HDFS创建目录,并上传要做测试的数据 三.开发SparkStreaming代码 四.运行代码后,往HDFS文件夹 ...

  7. go实现SnowFlake

    package main import ( "errors" "fmt" "strconv" "sync" " ...

  8. JS实现手机号码中间4位变星号

    这个问题,我们可以用截取字符串解决,以下我列出2种方法,小伙伴们可以根据自己的需要选择哦: ● 1,substring()方法用于提取字符串中介于两个指定下标之间的字符. '; //该号码是乱打出来的 ...

  9. 【Poj-3693】Maximum repetition substring 后缀数组 连续重复子串

    POJ - 3693 题意 SPOJ - REPEATS的进阶版,在这题的基础上输出字典序最小的重复字串. 思路 跟上题一样,先求出最长的重复次数,在求的过程中顺便纪录最多次数可能的长度. 因为sa数 ...

  10. 帝国cms 批量替换 字段内容包含的 指定的 关键字 SQL命令

    帝国cms 批量替换 字段内容包含的 指定的 关键字update phome_ecms_news_data_1 set newstext=replace(newstext,'原来','现在');