Reputation: 691
My QFormLayout formLayout
contains 3 QLineEdit widgets (leEntry1
, leEntry2
and leEntry3
). I want to replace leEntry2
with QComboBox cbOptions
, if the value of leEntry1
is "combo."
I managed to add a combo box after leEntry3
, but when I tried to use addWidget
with row and column parameters:
self.formLayout.addWidget(self.cbOptions, 1, 1)
I got the following error message:
TypeError: addWidget(self, QWidget): too many arguments
How do I need to change the code to dynamically replace leEntry2
with cbOptions
and vice versa?
Here's my code.
gui.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>215</width>
<height>133</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="lbLabel1">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Entry 1:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="leEntry1">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbLabel2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Entry 2:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="leEntry2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lbLabel3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Entry 3</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="leEntry3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QDialog, QComboBox, QApplication
class GUI(QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'gui.ui'), self)
self.buttonBox.rejected.connect(self.reject)
self.leEntry1.textChanged.connect(self.text_changed)
self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
def text_changed(self):
le1_value = self.leEntry1.text()
if le1_value == 'combo':
self.leEntry2.hide()
self.cbOptions = QComboBox()
self.cbOptions.addItems(['option 1', 'option 2', 'option3'])
self.formLayout.addWidget(self.cbOptions)
#self.formLayout.addWidget(self.cbOptions, 1, 1)
# TypeError: addWidget(self, QWidget): too many arguments
else:
self.leEntry2.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 1128
Reputation: 13651
What he wrote @ekhumoro looks like this:
import sys, os
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QDialog, QComboBox, QApplication
class GUI(QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'gui.ui'), self)
self.buttonBox.rejected.connect(self.reject)
self.leEntry1.textChanged.connect(self.text_changed)
self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
def text_changed(self):
le1_value = self.leEntry1.text()
if le1_value == 'combo':
self.formLayout.removeRow(self.leEntry2) # <---
self.cbOptions = QComboBox()
self.cbOptions.addItems(['option 1', 'option 2', 'option3'])
self.formLayout.insertRow(1, "cbOptions", self.cbOptions) # <---
if __name__ == '__main__':
app = QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
Upvotes: 2