My QFormLayout formLayout
содержит 3 виджета QLineEdit (leEntry1
, leEntry2
и leEntry3
). Я хочу заменить leEntry2
на QComboBox cbOptions
, если значение leEntry1
является "комбинированным".
Мне удалось добавить поле со списком после leEntry3
, но когда я попытался использовать addWidget
с параметрами строки и столбца:
self.formLayout.addWidget(self.cbOptions, 1, 1)
Я получил следующее сообщение об ошибке:
TypeError: addWidget(self, QWidget): too many arguments
Как мне нужно изменить код для динамической замены leEntry2
на cbOptions
и наоборот?
Вот мой код.
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_())