import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class ComboxDemo(QWidget):
    def __init__(self, parent=None):
        super(ComboxDemo, self).__init__(parent)
        self.setWindowTitle("combox 例子") 
        self.resize(300, 90) 
        layout = QVBoxLayout()
        self.lbl = QLabel("" ) 

        self.cb = QComboBox()
        self.cb.addItem("C")
        self.cb.addItem("C++")
        self.cb.addItems(["Java", "C#", "Python"])
        self.cb.currentIndexChanged.connect(self.selectionchange)
        layout.addWidget(self.cb)
        layout.addWidget(self.lbl )
        self.setLayout(layout)

    def selectionchange(self,i):
        self.lbl.setText( self.cb.currentText() )
        self.lbl.adjustSize()

        print( "Items in the list are :" )
        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_())

QCombox树状结构

import sys
from pprint import pprint

from PyQt5.Qt import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QComboBox-模型操作视图操作")
        self.resize(500, 500)
        self.move(400, 250)
        self.setup_ui()

    def setup_ui(self):
        cbb = QComboBox(self)
        cbb.move(100, 100)
        cbb.resize(200, 20)

        model = QStandardItemModel()

        item_1 = QStandardItem("item_1")
        item_2 = QStandardItem("item_2")
        item_22 = QStandardItem("item_22")
        item_2.appendRow(item_22)
        model.appendRow(item_1)
        model.appendRow(item_2)

        cbb.setModel(model)
    
        cbb.setView(QTreeView(cbb))

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

原文地址:https://blog.csdn.net/geji001/article/details/134700760

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_32644.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注