Disable or hide QLineEdit if one option from the QComboBox is active
-
Hi! It is my first day and I am trying to hide one QLineEdit while one option from the QComboBox is selected. I tried many things but without luck. Maybe can I get here some help.
Here is the code. I just want to hide the QLineEdit self.edit_winkel if the combobox is showing "Kehlnaht".Thanks!
from operator import index import sys from cmath import pi import math from tkinter import E from PySide6.QtWidgets import (QLineEdit, QPushButton, QApplication, QVBoxLayout, QDialog, QFontComboBox, QComboBox,QHBoxLayout,QLabel,QGridLayout,QGroupBox,QTextEdit) from matplotlib import widgets class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) # Create widgets self.edit_dicke = QLineEdit("") self.edit_V = QLineEdit("") self.edit_laenge = QLineEdit("") self.edit_winkel = QLineEdit("") self.button = QPushButton("Berechnen") self.label_dicke = QLabel("a [mm] = ") self.label_Naht = QLabel("Naht ") self.label_V = QLabel("V [mm] = ") self.label_laenge = QLabel("L [m] = ") self.label_winkel = QLabel("Winkel [°] = ") self.combobox_Nahtselector = QComboBox() self.combobox_Nahtselector.addItem("Kehlnaht") self.combobox_Nahtselector.addItem("V-Naht") self.combobox_Nahtselector.addItem("HV-Naht") self.gb = QTextEdit() # Create layout and add widgets layout = QGridLayout() layout.addWidget(self.label_Naht,0,0) layout.addWidget(self.combobox_Nahtselector,0,2) layout.addWidget(self.label_dicke,1,0) layout.addWidget(self.edit_dicke,1,2) layout.addWidget(self.label_V,2,0) layout.addWidget(self.edit_V,2,2) layout.addWidget(self.label_winkel,3,0) layout.addWidget(self.edit_winkel,3,2) layout.addWidget(self.label_laenge,4,0) layout.addWidget(self.edit_laenge,4,2) layout.addWidget(self.button,5,2) layout.addWidget(self.gb,6,0,6,3) # Set dialog layout self.setLayout(layout) # finding the content of current item in combo box content = self.combobox_Nahtselector.currentText() # self.combobox_Nahtselector.currentIndexChanged.connect(self.combobox) def currentIndexChanged(self): if self.combobox_Nahtselector.currentText == "Kehlnaht": self.edit_winkel.setEnabled(False) else: self.edit_winkel.setEnabled(True) if __name__ == '__main__': # Create the Qt Application app = QApplication(sys.argv) # Create and show the form form = Form() form.show() # Run the main Qt loop sys.exit(app.exec())
-
Hi! It is my first day and I am trying to hide one QLineEdit while one option from the QComboBox is selected. I tried many things but without luck. Maybe can I get here some help.
Here is the code. I just want to hide the QLineEdit self.edit_winkel if the combobox is showing "Kehlnaht".Thanks!
from operator import index import sys from cmath import pi import math from tkinter import E from PySide6.QtWidgets import (QLineEdit, QPushButton, QApplication, QVBoxLayout, QDialog, QFontComboBox, QComboBox,QHBoxLayout,QLabel,QGridLayout,QGroupBox,QTextEdit) from matplotlib import widgets class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) # Create widgets self.edit_dicke = QLineEdit("") self.edit_V = QLineEdit("") self.edit_laenge = QLineEdit("") self.edit_winkel = QLineEdit("") self.button = QPushButton("Berechnen") self.label_dicke = QLabel("a [mm] = ") self.label_Naht = QLabel("Naht ") self.label_V = QLabel("V [mm] = ") self.label_laenge = QLabel("L [m] = ") self.label_winkel = QLabel("Winkel [°] = ") self.combobox_Nahtselector = QComboBox() self.combobox_Nahtselector.addItem("Kehlnaht") self.combobox_Nahtselector.addItem("V-Naht") self.combobox_Nahtselector.addItem("HV-Naht") self.gb = QTextEdit() # Create layout and add widgets layout = QGridLayout() layout.addWidget(self.label_Naht,0,0) layout.addWidget(self.combobox_Nahtselector,0,2) layout.addWidget(self.label_dicke,1,0) layout.addWidget(self.edit_dicke,1,2) layout.addWidget(self.label_V,2,0) layout.addWidget(self.edit_V,2,2) layout.addWidget(self.label_winkel,3,0) layout.addWidget(self.edit_winkel,3,2) layout.addWidget(self.label_laenge,4,0) layout.addWidget(self.edit_laenge,4,2) layout.addWidget(self.button,5,2) layout.addWidget(self.gb,6,0,6,3) # Set dialog layout self.setLayout(layout) # finding the content of current item in combo box content = self.combobox_Nahtselector.currentText() # self.combobox_Nahtselector.currentIndexChanged.connect(self.combobox) def currentIndexChanged(self): if self.combobox_Nahtselector.currentText == "Kehlnaht": self.edit_winkel.setEnabled(False) else: self.edit_winkel.setEnabled(True) if __name__ == '__main__': # Create the Qt Application app = QApplication(sys.argv) # Create and show the form form = Form() form.show() # Run the main Qt loop sys.exit(app.exec())
@TitoElan said in Disable or hide QLineEdit if one option from the QComboBox is active:
# finding the content of current item in combo box content = self.combobox_Nahtselector.currentText() # self.combobox_Nahtselector.currentIndexChanged.connect(self.combobox)
Hello and weclome.
The uncommented line won't help you, as it only tells you what the value is while you are inside
__init__()
. The commented line is indeed what you will need: connecting the signal to your slot. But aren't your parameters wrong? Don't you want:self.combobox_Nahtselector.currentIndexChanged.connect(self.currentIndexChanged)
You connect the combobox's
currentIndexChanged
signal to a slot method you write, hereForm
'scurrentIndexChanged()
.When that's working, you might rename
def currentIndexChanged(self):
todef combobox_NahtselectorCurrentIndexChanged(self)
because that what it is a slot for and might make it clearer to you what is going on. -
@TitoElan said in Disable or hide QLineEdit if one option from the QComboBox is active:
# finding the content of current item in combo box content = self.combobox_Nahtselector.currentText() # self.combobox_Nahtselector.currentIndexChanged.connect(self.combobox)
Hello and weclome.
The uncommented line won't help you, as it only tells you what the value is while you are inside
__init__()
. The commented line is indeed what you will need: connecting the signal to your slot. But aren't your parameters wrong? Don't you want:self.combobox_Nahtselector.currentIndexChanged.connect(self.currentIndexChanged)
You connect the combobox's
currentIndexChanged
signal to a slot method you write, hereForm
'scurrentIndexChanged()
.When that's working, you might rename
def currentIndexChanged(self):
todef combobox_NahtselectorCurrentIndexChanged(self)
because that what it is a slot for and might make it clearer to you what is going on. -
@JonB thanks! I'm not sure how, but it works now. I guess I need to read some documentation about the signals and slots. :D
@TitoElan said in Disable or hide QLineEdit if one option from the QComboBox is active:
I guess I need to read some documentation about the signals and slots. :D
Understatement! You won't get anywhere in Qt with understanding signal & slots. Read through https://doc.qt.io/qt-5/signalsandslots.html and https://wiki.qt.io/Qt_for_Python_Signals_and_Slots. For Python there are slight differences (syntax) between PyQt vs PySide, so decide which you are using.