如何在Pyqt5中从QComboBox中选择一个选项时动态添加小部件

时间:2018-01-28 16:00:57

标签: python pyqt pyqt5

当用户从QComboBox中选择特定项目时,我想在GUI中添加小部件。

使用组合框 Pip config 中的不同选项,我希望GUI看起来如下图所示。在右图中,项目 Multi pip 有额外的小部件。此外,我想要额外小部件的位置,如右图所示。

GUI_1 GUI_2

如何动态添加这些小部件?请找到以下代码。

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

1 个答案:

答案 0 :(得分:0)

我建议你设置小部件并将它们放在开头,就像你拥有它们一样,但是将它们设置为不可见。然后制作一个方法,根据qcombobox的当前文本设置可见的小部件,并将其连接到qcombobox的激活信号。

您还需要在几乎每个对象的前面添加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)

通过将项目设置为不可见而不是使用此方法添加它们,您还可以将它们设置为在cobobox的位置不适合它们时不可见。