简体   繁体   English

如何从Pyqt5中的QComboBox中选择选项后动态添加小部件

[英]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. 当用户从QComboBox选择特定项目时,我想在GUI中添加小部件。

With the different options in combo-box Pip config , I want GUI to look like as in the following images. 通过组合框Pip config中的不同选项,我希望GUI如下图所示。 In the right image, there are extra widgets present for an item Multi pip . 在右边的图像中,有一个用于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. 然后使一个方法根据qcombobox的当前文本设置适当的小部件可见,并将其连接到qcombobox的激活信号。

You will also need to add self in front of almost every object so that it can be referred to from other methods. 您还需要在几乎每个对象的前面添加self ,以便可以从其他方法中引用它。

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. 通过将项目设置为不可见而不是使用此方法添加它们,还可以将其设置为在cobobox的位置不适合它们时不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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