• Pysied6 ComboBox



    Pyside6的ComboBox下拉列表框,可以给用户提供一系列的选项,下面就来简单了解一下Pysied6 ComboBox的使用。更多关于ComboBox的使用可以参考下面的文档。

    https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QComboBox.html

    Pysied6 ComboBox

    ComboBox常用函数

    函数作用
    addItem添加一个下拉选项
    addItems添加多个下拉选项
    currentIndex返回当前的下拉选项索引
    currentText返回当前下拉选项文本
    count返回下拉列表框中全部选项的个数
    removeItem删除选项
    clear清空所有选项
    insertItem将选项添加到指定的index位置
    setCurrentIndex显示指定index位置的选项

    ComboBox常用信号

    信号作用
    currentIndexChanged当下拉列表选项改变的时候触发

    例程

    ComboBox添加选项

    ComboBox添加选项有两种方式,分别是程序设置和界面设置

    程序设置
      self.ui.comboBox.addItem("苹果") # 添加单个选项
      self.ui.comboBox.addItems(["葡萄","香蕉","西瓜"]) # 添加多个选项
    
    • 1
    • 2
    界面设置

    打开designer软件,双击ComboBox控件,通过"+ -"符号添加和删除选项。
    在这里插入图片描述

    返回选项信息

    print(self.ui.comboBox.currentIndex()) # 打印出当前的选项
    print(self.ui.comboBox.currentText())  # 打印出当前选项的文本
    print(self.ui.comboBox.count())        # 打印出下拉列表框的选项个数
    
    • 1
    • 2
    • 3

    添加删除选项

    def additem_func(self):
            self.ui.comboBox.insertItem(0,str(datetime.now())) # 添加单个选项,并设置在index为0的位置
            self.ui.comboBox.setCurrentIndex(0)                # 显示index为0的选项
    def deleteitem_func(self):
            self.ui.comboBox.removeItem(self.ui.comboBox.currentIndex()) # 移除选项
    def combobox_index_change_func(self,index):
            print(self.ui.comboBox.currentText(),index) # 打印出选中的选项文本
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    完整程序

    界面程序
    
    <ui version="4.0">
     <class>MainWindowclass>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0x>
        <y>0y>
        <width>386width>
        <height>283height>
       rect>
      property>
      <property name="windowTitle">
       <string>MainWindowstring>
      property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <layout class="QVBoxLayout" name="verticalLayout">
          <property name="leftMargin">
           <number>50number>
          property>
          <item>
           <widget class="QComboBox" name="comboBox">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
              <horstretch>98horstretch>
              <verstretch>0verstretch>
             sizepolicy>
            property>
            <property name="maximumSize">
             <size>
              <width>250width>
              <height>20height>
             size>
            property>
            <property name="sizeIncrement">
             <size>
              <width>98width>
              <height>100height>
             size>
            property>
            <property name="baseSize">
             <size>
              <width>100width>
              <height>0height>
             size>
            property>
           widget>
          item>
         layout>
        item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout">
          <item>
           <widget class="QPushButton" name="deleteitem_btn">
            <property name="maximumSize">
             <size>
              <width>100width>
              <height>16777215height>
             size>
            property>
            <property name="text">
             <string>删除选项string>
            property>
           widget>
          item>
          <item>
           <widget class="QPushButton" name="additem_btn">
            <property name="maximumSize">
             <size>
              <width>100width>
              <height>16777215height>
             size>
            property>
            <property name="text">
             <string>增加选项string>
            property>
           widget>
          item>
         layout>
        item>
       layout>
      widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0x>
         <y>0y>
         <width>386width>
         <height>22height>
        rect>
       property>
      widget>
      <widget class="QStatusBar" name="statusbar"/>
     widget>
     <resources/>
     <connections/>
    ui>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    主程序
    # Import Qt libraries
    from PySide6.QtWidgets import *
    from PySide6.QtCore import QFile
    # Import UI developed in Qt Creator
    from combobox_ui import Ui_MainWindow  # 导入界面
    # Import PseudoSensor
    # Import system tools and datetime
    import sys
    import statistics
    import time
    from datetime import datetime
    
    # Create and start the Qt application
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            
            # 设置界面为用户设计的界面
            self.ui = Ui_MainWindow() 
            self.ui.setupUi(self) 
    
            self.ui.comboBox.addItem("苹果") # 添加单个选项
            self.ui.comboBox.addItems(["葡萄","香蕉","西瓜"]) # 添加多个选项
    
    
    
            print(self.ui.comboBox.currentIndex()) # 打印出当前的选项
            print(self.ui.comboBox.currentText())  # 打印出当前选项的文本
            print(self.ui.comboBox.count())        # 打印出下拉列表框的选项个数
          
    
            self.ui.comboBox.currentIndexChanged.connect(self.combobox_index_change_func)
    
            self.ui.additem_btn.clicked.connect(self.additem_func)
            self.ui.deleteitem_btn.clicked.connect(self.deleteitem_func)
    
        def additem_func(self):
            self.ui.comboBox.insertItem(0,str(datetime.now())) # 添加单个选项,并设置在index为0的位置
            self.ui.comboBox.setCurrentIndex(0)                # 显示index为0的选项
    
        def deleteitem_func(self):
            self.ui.comboBox.removeItem(self.ui.comboBox.currentIndex()) # 移除选项
    
        def combobox_index_change_func(self,index):
            print(self.ui.comboBox.currentText(),index) # 打印出选中的选项文本
    
           
        def closeAndExit(self):
            sys.exit()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv) # 初始化QApplication
    
        # 初始化界面并显示界面
        window = MainWindow() 
        window.show() 
        window.setFixedSize(window.width(), window.height())
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    在这里插入图片描述

  • 相关阅读:
    【CSH 入门基础 9 -- 输出 csh 脚本中每一句命令】
    【云原生网关】Kong 使用详解
    高通导航器软件开发包使用指南(15)
    BSPHP 未授权访问 信息泄露
    Java高级编程day22【谷】
    Mysql和ES数据同步方案汇总
    【Linux】实验二 Makefile 的编写及应用
    Springboot项目通过filter修改接口的入参
    如何在移动端猎豹浏览器中设置代理IP
    ANIMALS FULL PACK (总共三十个动物)
  • 原文地址:https://blog.csdn.net/hwx1546/article/details/133920996