• Pyside6/Qt实现下拉复选框


    效果:

    源码: 

    1. from PySide6.QtWidgets import QComboBox, QListWidget, QCheckBox, QListWidgetItem, QLineEdit, QApplication, QHBoxLayout, \
    2. QWidget
    3. from PySide6.QtGui import Qt
    4. from PySide6.QtCore import Signal
    5. # 自定义控件-点击按钮下拉CheckBox列表
    6. class XCombobox(QComboBox):
    7. # 有item被选择时,发出信号
    8. itemChecked = Signal(list)
    9. # 保存QCheckBox控件
    10. _checks = []
    11. def __init__(self, parent) -> None:
    12. super().__init__(parent)
    13. # 通过setView()设置下拉列表控件,通过setLineEdit替换原有的文本显示控件,并设置为只读模式。预设"全选"选项。
    14. listwgt = QListWidget(self)
    15. self.setView(listwgt)
    16. self.setModel(listwgt.model())
    17. lineEdit = QLineEdit(self)
    18. lineEdit.setReadOnly(True)
    19. self.setLineEdit(lineEdit)
    20. # 预设'全选'
    21. self.add_item('全选')
    22. def add_item(self, text: str):
    23. # 在这里给每个CheckBox都绑定了信号,目的是每次Check都能作出响应。
    24. check = QCheckBox(text, self.view())
    25. check.stateChanged.connect(self.on_state_changed)
    26. self._checks.append(check)
    27. item = QListWidgetItem(self.view())
    28. # 设置item不可选中(只可以被Check)
    29. item.setFlags(item.flags() & Qt.IntersectsItemShape)
    30. self.view().addItem(item)
    31. self.view().setItemWidget(item, check)
    32. def add_items(self, texts: list):
    33. for text in texts:
    34. self.add_item(text)
    35. def clear(self):
    36. self.view().clear()
    37. def get_selected(self):
    38. sel_data = []
    39. for chk in self._checks:
    40. if self._checks[0] == chk:
    41. continue
    42. if chk.checkState() == Qt.Checked:
    43. sel_data.append(chk.text())
    44. return sel_data
    45. def set_all_state(self, state):
    46. for chk in self._checks:
    47. chk.blockSignals(True)
    48. chk.setCheckState(Qt.CheckState(state))
    49. chk.blockSignals(False)
    50. def on_state_changed(self, state):
    51. if self.sender() == self._checks[0]:
    52. self.set_all_state(state)
    53. sel_data = self.get_selected()
    54. self.itemChecked.emit(sel_data)
    55. self.lineEdit().setText(';'.join(sel_data))
    56. def printa(data: list):
    57. print(data)
    58. if __name__ == '__main__':
    59. app = QApplication()
    60. wind = QWidget()
    61. wind.resize(500, 500)
    62. cmbox = XCombobox(wind)
    63. cmbox.add_items(['AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', 'DDD','AAA', 'BBB', 'CCC', '11DDD'])
    64. # cmbox.itemChecked.connect(printa)
    65. hyout = QHBoxLayout(wind)
    66. hyout.addWidget(cmbox)
    67. wind.setLayout(hyout)
    68. wind.show()
    69. app.exec()

  • 相关阅读:
    Linux 禁止用户或 IP通过 SSH 登录
    云原生周刊:Istio 1.19 发布 | 2023.9.11
    SLAM从入门到精通(dwa速度规划算法)
    vue 手势解锁功能
    OKR与个人成长:生活中的 OKR
    hive变更数据过程
    不用 Spring 居然连最基本的接口都不会写了!
    大数据—数据透析表常见使用(手把手详解)
    sqlserver management studio(2008R2版本)添加注释列及默认值列
    【牛客网-公司真题-前端入门篇】——奇安信秋招笔试-前端-卷2
  • 原文地址:https://blog.csdn.net/summerriver1/article/details/127047012