PyQt基础_007_ 按钮类控件QCombox

PyQt基础_007_ 按钮类控件QCombox_第1张图片

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树状结构

PyQt基础_007_ 按钮类控件QCombox_第2张图片

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_())

你可能感兴趣的:(PyQt,pyqt)