简体   繁体   中英

How to add widgets dynamically upon selecting an option from QComboBox in Pyqt5

I want to add widgets in GUI when a user selects a particular item from QComboBox.

With the different options in combo-box Pip config , I want GUI to look like as in the following images. In the right image, there are extra widgets present for an item Multi pip . Also I want the location of the extra widgets as shown in the right image.

GUI_1 GUI_2

How to add these widgets dynamically ? Please find the code below.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, QRect

class Example(QWidget): 

def __init__(self):
    super(Example, self).__init__()        
    self.initUI()        

def initUI(self):

    vbox = QVBoxLayout()

    CpsLabel = QLabel()
    CpsLabel.setText("<font size = 12>Cps</font>")
    CpsLabel.setAlignment(Qt.AlignCenter)
    CpsLabel.setTextFormat(Qt.RichText)
    CpsPipConfigLabel = QLabel('Pip config:    ')
    CpsPipConfigComboBox = QComboBox()
    CpsPipConfigComboBox.addItems(['Single pip', 'Dual pip', 'Multi pip'])
    CpsPipConfigComboBox.setCurrentIndex(2)
    CpsChannel = QLabel('Cps channel:    ')
    CpsChannelComboBox = QComboBox()
    CpsChannelComboBox.addItems(['A', 'B', 'C', 'D'])  
    CpsChannelComboBox.setCurrentIndex(0) 
    CpsTotalTeethLabel = QLabel('Total teeth:    ')             
    CpsTotalTeethEdit = QLineEdit()
    CpsTotalTeethEdit.setFixedWidth(50)
    CpsTotalTeethEdit.setPlaceholderText('18')
    CpsTotalTeethEdit.setValidator(QIntValidator())                
    CpsMissingTeethLabel = QLabel('Missing teeth:    ')             
    CpsMissingTeethEdit = QLineEdit()
    CpsMissingTeethEdit.setFixedWidth(50)
    CpsMissingTeethEdit.setPlaceholderText('1')
    CpsMissingTeethEdit.setValidator(QIntValidator())                           

    vbox.addWidget(CpsLabel)
    vbox.addStretch()

    CpsQHBox1 = QHBoxLayout()
    CpsQHBox1.setSpacing(0)
    CpsQHBox1.addStretch()
    CpsQHBox1.addWidget(CpsPipConfigLabel)
    CpsQHBox1.addWidget(CpsPipConfigComboBox)
    CpsQHBox1.addStretch()
    vbox.addLayout(CpsQHBox1)
    vbox.addStretch()

    CpsQHBox2 = QHBoxLayout()
    CpsQHBox2.setSpacing(0)
    CpsQHBox2.addStretch()
    CpsQHBox2.addSpacing(20)
    CpsQHBox2.addWidget(CpsTotalTeethLabel)
    CpsQHBox2.addWidget(CpsTotalTeethEdit)
    CpsQHBox2.addStretch()
    CpsQHBox2.addWidget(CpsMissingTeethLabel)
    CpsQHBox2.addWidget(CpsMissingTeethEdit)        
    CpsQHBox2.addStretch()
    vbox.addLayout(CpsQHBox2)
    vbox.addStretch()       

    CpsQHBox3 = QHBoxLayout()
    CpsQHBox3.setSpacing(0)
    CpsQHBox3.addStretch()
    CpsQHBox3.addWidget(CpsChannel)
    CpsQHBox3.addWidget(CpsChannelComboBox)
    CpsQHBox3.addStretch()
    vbox.addLayout(CpsQHBox3)
    vbox.addStretch()        

    self.setLayout(vbox)
    self.setGeometry(200, 100, 300, 300)
    self.setWindowTitle('Steady state data processing') 
    self.setWindowIcon(QIcon('duty_vs_suction_map_sum.png'))                    

    self.setAutoFillBackground(True)
    p = self.palette()
    p.setColor(self.backgroundRole(), QColor(255,250,100))
    # p.setColor(self.backgroundRole(), Qt.blue)
    self.setPalette(p)
    self.show()

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()    
sys.exit(app.exec_())

I suggest you set the widgets up and place them at the beginning like you have them, but set them invisible. Then make a method that sets the appropriate widgets visible based on the qcombobox's current text and connect it to the qcombobox's activated signal.

You will also need to add self in front of almost every object so that it can be referred to from other methods.

class Example(QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        # setup code here...

        self.CpsTotalTeethEdit.setVisible(False)
        self.CpsTotalTeethLabel.setVisible(False)

        self.CpsPipConfigComboBox.activated.connect(self.setup_total_teeth)

        self.show()

    def setup_widgets(self):
        if self.CpsPipConfigComboBox.currentText() == "Multi pip":
            self.CpsTotalTeethLabel.setVisible(True)
            self.CpsTotalTeethEdit.setVisible(True)

By setting the items invisible instead of adding them with this method, you can also set them to be not visible when the cobobox's position is not for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM