代码之家  ›  专栏  ›  技术社区  ›  Nemo XXX

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

  •  0
  • Nemo XXX  · 技术社区  · 6 年前

    我的QformLayout formLayout 包含3个qlineedit小部件( leEntry1 , leEntry2 leEntry3 )。我想替换 LeCyTy2 带qcombobox cbOptions ,如果 LeNexy1 是“组合”。 我设法添加了一个组合框 之后 小精灵 ,但当我试图使用 addWidget 使用行和列参数:

    self.formLayout.addWidget(self.cbOptions, 1, 1)
    

    我收到以下错误消息:

    TypeError: addWidget(self, QWidget): too many arguments
    

    如何更改代码以动态替换 LeCyTy2 具有 CB期权 反之亦然?

    这是我的密码。

    GUI

    <?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>
    

    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 回复  |  直到 6 年前
        1
  •  1
  •   S. Nick    6 年前

    他写的“ekhuroo”是这样的:

    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