如何替换现有QFormLayout中的小部件?

时间:2019-01-13 14:10:19

标签: python-3.x pyqt5 qcombobox qlineedit qformlayout

我的QFormLayout formLayout包含3个QLineEdit小部件(leEntry1leEntry2leEntry3)。如果leEntry2的值为“ combo”,我想用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_())

1 个答案:

答案 0 :(得分:1)

他写的@ekhumoro看起来像这样:

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_())

enter image description here