PyQt5下拉式复选框QComboCheckBox的实例

yipeiwu_com6年前Python基础

笔者在用PyQt5写GUI时碰到了需要使用下拉式复选框的情况,但是PyQt5中没有相应的组件,而网上找到的方法大多是qt使用的,所以不能直接拿来用。

没办法,在这种让人无奈的情况下,笔者只能根据网上大神们的方法试着自己写一个喽。

你还别说,真就让我写出来了。(笔者是个菜鸟新手,所以这小小的成功让我很开心)

然后笔者就很严肃地将这个组件命名为QComboCheckBox,也就是QComboBox和QCheckBox的拼接。

废话不多说,直接先上效果图:

然后是代码:(第一个是基础,第二个是带全选和清空功能)

from PyQt5.QtWidgets import QComboBox,QLineEdit,QListWidget,QCheckBox,QListWidgetItem
 
class ComboCheckBox(QComboBox):
  def __init__(self,items):#items==[str,str...]
    super(ComboCheckBox,self).__init__()
    self.items=items
    self.qCheckBox=[]
    self.qLineEdit=QLineEdit()
    self.qLineEdit.setReadOnly(True)
    qListWidget=QListWidget()
 
    self.row_num=len(self.items)
    for i in range(self.row_num):
      self.qCheckBox.append(QCheckBox())
      qItem=QListWidgetItem(qListWidget)
      self.qCheckBox[i].setText(self.items[i])
      qListWidget.setItemWidget(qItem,self.qCheckBox[i])
      self.qCheckBox[i].stateChanged.connect(self.show)
    
    self.setLineEdit(self.qLineEdit)     
    self.setModel(qListWidget.model())
    self.setView(qListWidget)
 
  def Selectlist(self):
    Outputlist=[]
    for i in range(self.row_num):
      if self.qCheckBox[i].isChecked()==True:
        Outputlist.append(self.qCheckBox[i].text())
    return Outputlist
 
  def show(self):
    show=''
    self.qLineEdit.setReadOnly(False)
    self.qLineEdit.clear()
    for i in self.Selectlist():
      show+=i+';'
    self.qLineEdit.setText(show)
    self.qLineEdit.setReadOnly(True)
from PyQt5.QtWidgets import QComboBox,QLineEdit,QListWidget,QCheckBox,QListWidgetItem
 
class ComboCheckBox(QComboBox):
  def __init__(self,items):#items==[str,str...]
    super(ComboCheckBox,self).__init__()
    self.items=items
    self.items.insert(0,'全部')
    self.row_num=len(self.items)
    self.Selectedrow_num=0
    self.qCheckBox=[]
    self.qLineEdit=QLineEdit()
    self.qLineEdit.setReadOnly(True)
    self.qListWidget=QListWidget()
    self.addQCheckBox(0)
    self.qCheckBox[0].stateChanged.connect(self.All)
    for i in range(1,self.row_num):
      self.addQCheckBox(i)
      self.qCheckBox[i].stateChanged.connect(self.show)
    self.setModel(self.qListWidget.model())
    self.setView(self.qListWidget)
    self.setLineEdit(self.qLineEdit)     
 
  def addQCheckBox(self,i):
    self.qCheckBox.append(QCheckBox())
    qItem=QListWidgetItem(self.qListWidget)
    self.qCheckBox[i].setText(self.items[i])
    self.qListWidget.setItemWidget(qItem,self.qCheckBox[i])    
 
  def Selectlist(self):
    Outputlist=[]
    for i in range(1,self.row_num):
      if self.qCheckBox[i].isChecked()==True:
        Outputlist.append(self.qCheckBox[i].text())
    self.Selectedrow_num=len(Outputlist)
    return Outputlist 
 
  def show(self):
    show=''
    Outputlist=self.Selectlist()
    self.qLineEdit.setReadOnly(False)
    self.qLineEdit.clear()
    for i in Outputlist:
      show+=i+';'
    if self.Selectedrow_num==0:
      self.qCheckBox[0].setCheckState(0)
    elif self.Selectedrow_num==self.row_num-1:
      self.qCheckBox[0].setCheckState(2)
    else:
      self.qCheckBox[0].setCheckState(1)
    self.qLineEdit.setText(show)
    self.qLineEdit.setReadOnly(True)
 
  def All(self,zhuangtai):
    if zhuangtai==2:
      for i in range(1,self.row_num):
        self.qCheckBox[i].setChecked(True)
    elif zhuangtai==1:
      if self.Selectedrow_num==0:
        self.qCheckBox[0].setCheckState(2)
    elif zhuangtai==0:
      self.clear()
 
  def clear(self):
    for i in range(self.row_num):
      self.qCheckBox[i].setChecked(False)

使用方法:

a=ComboCheckBox('子项列表')
Selectlist()获取被选子项列表
All()全选
clear()清空已选项

以上这篇PyQt5下拉式复选框QComboCheckBox的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python基于SMTP协议实现发送邮件功能详解

Python基于SMTP协议实现发送邮件功能详解

本文实例讲述了Python基于SMTP协议实现发送邮件功能。分享给大家供大家参考,具体如下: SMTP(Simple Mail Transfer Protocol),即简单邮件传输协议,...

Python判断telnet通不通的实例

这个跟ping那个差不多,ping的那个脚本就是通过这个改了下,大体一致,不过telnet的不需要判断返回的字符串。快一些 这里具体需要telnet的ip是需要自己向定义好的数组中写的...

用Python写的图片蜘蛛人代码

复制代码 代码如下:#coding=utf-8 import os import sys import re import urllib URL_REG = re.compile(r'(...

ipython和python区别详解

ipython和python区别详解

ipython介绍 IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell命令,内...

解决python中画图时x,y轴名称出现中文乱码的问题

解决python中画图时x,y轴名称出现中文乱码的问题

如下所示: #-*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from matplot...