给 QComboBox 下拉菜单每个项目后增加删除按钮

【代码】给 QComboBox 下拉菜单每个项目后增加删除按钮。python pyside6 pyqt
摘要由CSDN通过智能技术生成

import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox, QHBoxLayout, QPushButton

class myComboBox:
    def __init__(self, _comboBox):
        self.comboBox = _comboBox
        self.comboBox.setModel(QStandardItemModel(self.comboBox))

    def addItem(self,itemTxt):
        QS_item = QStandardItem(itemTxt)
        self.comboBox.model().appendRow(QS_item)
        index = self.comboBox.count()-1
        self.add_btn(index, itemTxt)

    def add_btn(self, _index, _itemTxt):
        # 创建一个水平布局,并将标签和删除按钮添加到其中
        layout = QHBoxLayout()
        layout.setContentsMargins(75, 0, 0, 0)
        button = QPushButton('x')
        button.setFixedSize(20, 20)
        button.set
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您解答。 首先,我们需要利用QComboBox()制作下拉菜单,这个比较简单,您可以参考如下代码: ```python from PyQt5.QtWidgets import QApplication, QComboBox, QWidget class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.cb = QComboBox(self) self.cb.addItem('Button1') self.cb.addItem('Button2') self.cb.addItem('Button3') self.cb.move(50, 50) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QComboBox') self.show() ``` 这样我们就可以创建一个包含三个选项的下拉菜单。 接下来,我们需要将每个下拉菜单中的内容制成按钮,并实现点击按钮跳转的功能。代码如下: ```python from PyQt5.QtWidgets import QApplication, QComboBox, QWidget, QPushButton from PyQt5.QtCore import Qt class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.cb = QComboBox(self) self.cb.addItem('Button1') self.cb.addItem('Button2') self.cb.addItem('Button3') self.cb.move(50, 50) self.button1 = QPushButton('Button1', self) self.button1.move(50, 100) self.button1.hide() self.button2 = QPushButton('Button2', self) self.button2.move(50, 100) self.button2.hide() self.button3 = QPushButton('Button3', self) self.button3.move(50, 100) self.button3.hide() self.cb.activated[str].connect(self.onActivated) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QComboBox') self.show() def onActivated(self, text): if text == 'Button1': self.button1.show() self.button2.hide() self.button3.hide() elif text == 'Button2': self.button1.hide() self.button2.show() self.button3.hide() elif text == 'Button3': self.button1.hide() self.button2.hide() self.button3.show() ``` 在这个示例中,我们创建了三个按钮,并将它们隐藏起来。当用户在下拉菜单中选择某个选项时,根据选项的不同,显示相应的按钮,并隐藏其他按钮。 这样,当用户点击相应的按钮时,就可以实现跳转的功能了。 希望这个示例对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值