多复选框pyq

2024-04-25 10:10:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pyqt创建一个QGIS插件。我用qtdesigner创建了我的图形界面。我的问题是我在pyqt中找不到任何MuticheckCombobox小部件,所以我寻找了一种创建个性化小部件的方法,所以我创建了一个名为

class CheckableComboBox(QtGui.QComboBox):
    def __init__(self, parent=None):
        super(CheckableComboBox, self).__init__(parent)
        self.view().pressed.connect(self.handleItemPressed)
        self._changed = False

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)
        self._changed = True

    def hidePopup(self):
        if not self._changed:
            super(CheckableComboBox, self).hidePopup()
        self._changed = False

    def itemChecked(self, index):
        item = self.model().item(index, self.modelColumn())
        return item.checkState() == QtCore.Qt.Checked

    def setItemChecked(self, index, checked=True):
        item = self.model().item(index, self.modelColumn())
        if checked:
            item.setCheckState(QtCore.Qt.Checked)
        else:
            item.setCheckState(QtCore.Qt.Unchecked)

我的主要课程是这样的:

class pluginDialog2(QDialog):
    def __init__(self,parent,iface,par):
        """Constructor."""
        super(pluginDialog2, self).__init__(parent)
        self.ui=uic.loadUi(os.path.dirname(os.path.realpath(__file__))+'/form2.ui', self)
        self.parent=parent
        self.iface=iface
        self.combo = CheckableComboBox(self)
        for index in range(6):
            self.combo.addItem('Item %d' % index)
            self.combo.setItemChecked(index, False)

但我不知道如何将这个小部件添加到我的窗口中,我发现的所有示例都使用用python代码创建的图形界面,而不是qtdesigner软件

编辑:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ajouterCoucheDialogBase</class>
 <widget class="QDialog" name="ajouterCoucheDialogBase">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>772</width>
    <height>687</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Ajouter une couche</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout_3">
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <widget class="QLabel" name="label_4">
       <property name="text">
        <string/>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <widget class="QTabWidget" name="tabWidget">
         <property name="currentIndex">
          <number>0</number>
         </property>
         <widget class="QWidget" name="tab">
          <attribute name="title">
           <string>Documents d'urbanisme</string>
          </attribute>
          <widget class="QComboBox" name="comboBox">
           <property name="geometry">
            <rect>
             <x>420</x>
             <y>10</y>
             <width>231</width>
             <height>31</height>
            </rect>
           </property>
          </widget>
          <widget class="QLabel" name="label">
           <property name="geometry">
            <rect>
             <x>90</x>
             <y>20</y>
             <width>301</width>
             <height>17</height>
            </rect>
           </property>
           <property name="text">
            <string>Sélectionnez le périmètre par commune             :</string>
           </property>
          </widget>
          <widget class="QLabel" name="label_2">
           <property name="geometry">
            <rect>
             <x>90</x>
             <y>60</y>
             <width>301</width>
             <height>20</height>
            </rect>
           </property>
           <property name="text">
            <string>Sélectionnez les phases                                    :</string>
           </property>
          </widget>
          <widget class="QComboBox" name="comboBox_2">
           <property name="geometry">
            <rect>
             <x>420</x>
             <y>50</y>
             <width>231</width>
             <height>31</height>
            </rect>
           </property>
          </widget>
          <widget class="QTreeWidget" name="treeWidget">
           <property name="geometry">
            <rect>
             <x>20</x>
             <y>101</y>
             <width>711</width>
             <height>451</height>
            </rect>
           </property>
           <property name="columnCount">
            <number>1</number>
           </property>
           <attribute name="headerVisible">
            <bool>false</bool>
           </attribute>
           <column>
            <property name="text">
             <string notr="true">1</string>
            </property>
           </column>
          </widget>
         </widget>
         <widget class="QWidget" name="tab_2">
          <attribute name="title">
           <string>Restitutions</string>
          </attribute>
          <widget class="QTreeWidget" name="treeWidget_2">
           <property name="geometry">
            <rect>
             <x>20</x>
             <y>100</y>
             <width>711</width>
             <height>451</height>
            </rect>
           </property>
           <property name="columnCount">
            <number>1</number>
           </property>
           <attribute name="headerVisible">
            <bool>false</bool>
           </attribute>
           <column>
            <property name="text">
             <string notr="true">1</string>
            </property>
           </column>
          </widget>
          <widget class="QLabel" name="label_5">
           <property name="geometry">
            <rect>
             <x>90</x>
             <y>60</y>
             <width>301</width>
             <height>17</height>
            </rect>
           </property>
           <property name="text">
            <string>Sélectionnez la date de la restitution                  :</string>
           </property>
          </widget>
          <widget class="QComboBox" name="comboBox_4">
           <property name="geometry">
            <rect>
             <x>420</x>
             <y>50</y>
             <width>231</width>
             <height>31</height>
            </rect>
           </property>
          </widget>
          <widget class="QLabel" name="label_3">
           <property name="geometry">
            <rect>
             <x>90</x>
             <y>20</y>
             <width>301</width>
             <height>17</height>
            </rect>
           </property>
           <property name="text">
            <string>Sélectionnez le périmètre par commune             :</string>
           </property>
          </widget>
          <widget class="QComboBox" name="comboBox_3">
           <property name="geometry">
            <rect>
             <x>420</x>
             <y>10</y>
             <width>231</width>
             <height>31</height>
            </rect>
           </property>
          </widget>
         </widget>
         <widget class="QWidget" name="tab_3">
          <attribute name="title">
           <string>Autres</string>
          </attribute>
          <widget class="QTreeWidget" name="treeWidget_3">
           <property name="geometry">
            <rect>
             <x>20</x>
             <y>50</y>
             <width>711</width>
             <height>511</height>
            </rect>
           </property>
           <property name="columnCount">
            <number>1</number>
           </property>
           <attribute name="headerVisible">
            <bool>false</bool>
           </attribute>
           <column>
            <property name="text">
             <string notr="true">1</string>
            </property>
           </column>
          </widget>
         </widget>
        </widget>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <spacer name="horizontalSpacer_2">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QPushButton" name="disconnect">
           <property name="text">
            <string>déconnexion</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>0</width>
             <height>0</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QDialogButtonBox" name="buttonBox">
           <property name="standardButtons">
            <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer_3">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
        </layout>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

Tags: textnamerectselfstringindexattributeproperty