0

I am trying to create a textbox after selecting the textbox option from a combo box and adding an image after selecting the image option. I need help with this.

I know that self.line = QLineEdit will create a textbox and for a Combobox I can use combo.activated[str].connect(self.onChanged) to detect the change in combobox and call for the function but I dont know why it is not working after I put it in the onChanged function.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import * 

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
            
        combo = QComboBox(self)
        combo.addItem("textbox")
        combo.addItem("image")
    
        combo.move(50, 50)

        self.qlabel = QLabel(self)
        self.qlabel.move(50,16)

        combo.activated[str].connect(self.onChanged)      

        self.setGeometry(50,50,320,200)
        self.setWindowTitle("Programme")
        self.show()

    def onChanged(self, text):   
        if text == 'textbox':
            self.line = QLineEdit(self)
        if text == 'image':
            self.im = QPixmap("image.jpg")
            self.label = QLabel()
            self.label.setPixmap(self.im)
if __name__ == '__main__':
     app = QApplication(sys.argv)
     ex = Example()
     sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jin
  • 3
  • 2

1 Answers1

1

Items created with a parent that has already been shown are not displayed automatically, and show() or setVisible(True) must be explicitly called, unless they are added to a layout.

In your case, you need to add self.line.show(), or self.label.show(), but you also need to create the label with the parent argument: self.label = QLabel(self), otherwise it will be considered a top level widget and it will be shown on a separate window.

Consider that creating widgets like this can create some problems, especially if the selection is done more than once: if you want the new selection to overwrite the previous widget, you need to delete it, and in any case you should add the widget to a layout (which should be created anyway, as fixed geometries are generally discouraged). As explained above, adding a new widget to a layout will automatically show it, so there's no need to explicitly show it any more.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import * 

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Programme")

        central = QWidget()
        self.setCentralWidget(central)
        self.layout = QVBoxLayout(central)

        combo = QComboBox(self)
        combo.addItems(("textbox", "image"))
        self.layout.addWidget(combo)

        self.overwriteCheck = QCheckBox('Overwrite')
        self.layout.addWidget(self.overwriteCheck)
        self.lastWidget = None

        combo.activated[str].connect(self.onChanged)      
        self.show()

    def onChanged(self, text):
        if self.lastWidget and self.overwriteCheck.isChecked():
            self.layout.removeWidget(self.lastWidget)
            self.lastWidget.deleteLater()

        if text == 'textbox':
            self.lastWidget = QLineEdit()
        elif text == 'image':
            self.lastWidget = QLabel()
            self.lastWidget.setPixmap(QPixmap("image.jpg"))

        self.layout.addWidget(self.lastWidget)

        if self.overwriteCheck.isChecked():
            QApplication.processEvents()
            self.adjustSize()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
musicamante
  • 41,230
  • 6
  • 33
  • 58