0

Know, I try to make GUI by using pyqt. I want to hide some widget at the start of the program and show when I select Combobox. It's possible to hide some widget at the start of the program. But, it's impossible to show a specific widget when I select Combobox. Could you please give me the advice for resolving this?

class MyForm(QtGui.QWidget):
def __init__(self):
    QtGui.QWidget.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.oneLayerWidget.setVisible(False)
    self.ui.twoLayerWidget.setVisible(False)
    self.ui.layTypeComboBox.clear()
    self.ui.layTypeComboBox.addItems(["Original Layer", "Boolean Layer"])
    self.ui.layTypeComboBox.currentIndexChanged.connect(self.layTypeSelEvent)

def layTypeSelEvent(self):
    layType = str(self.ui.layTypeComboBox.currentText())
    if layType == "Original Layer":
        self.ui.twoLayerWidget.setVisible(False)
        self.ui.oneLayerWidget.setVisible(True)
    elif layType == "Boolean Layer":
        self.ui.oneLayerWidget.setVisible(False)
        self.ui.twoLayerWidget.setVisible(True)
Deepak Kumar
  • 1,246
  • 14
  • 38
MINWOO YU
  • 21
  • 3

2 Answers2

1
class MyForm(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.oneLayerWidget.setVisible(Ture)
        self.ui.twoLayerWidget.setVisible(False)
        self.ui.layTypeComboBox.clear()
        self.ui.layTypeComboBox.addItems(["Original Layer", "Boolean Layer"])
        self.ui.layTypeComboBox.currentIndexChanged.connect(self.layTypeSelEvent)

    def layTypeSelEvent(self, index):
        if index == 0:
            self.ui.twoLayerWidget.setVisible(False)
            self.ui.oneLayerWidget.setVisible(True)
        elif index == 1:
            self.ui.oneLayerWidget.setVisible(False)
            self.ui.twoLayerWidget.setVisible(True)
dudulu
  • 754
  • 6
  • 17
0

If i understand you correctly you want something like this to hide your widget self.ui.twoLayerWidget.hide() and show your widget self.ui.twoLayerWidget.show()

It is also similar to setVisible see this link

Or maybe you can consider using stackedWidget, you can change which widget to show out by changing the index

self.ui.stackedWidget.setCurrentIndex(1)
WenJuan
  • 624
  • 1
  • 8
  • 21
  • Yes, self.ui.twoLayerWidget.hide() and self.ui.twoLayerWidget.show() also work like setVisible. What i want to do is that widget hide and show when i changed combobox(layTypeCombobox). According to my code, when i select Original Layer of combobox, twoLayerWidget have to hide and oneLayerWidget have to show. However, it doesn't work. Always both widgets hide. I doubt whether self.ui.oneLayerWidget.setVisible(False) and self.ui.twoLayerWidget.setVisible(False) in def __init__(self): cause this problem. – MINWOO YU Aug 11 '20 at 06:57
  • maybe can consider using stacked widget for your case? – WenJuan Aug 11 '20 at 07:56
  • Yes! stacked widget might be what i want to make. – MINWOO YU Aug 11 '20 at 08:03
  • Please mark it if you think this is the solution for you :) – WenJuan Aug 11 '20 at 08:19