从QScrollArea访问孩子

时间:2017-01-10 10:16:45

标签: qt visual-c++

我已经在QDialog表单中添加了一个QScrollArea小部件。

在scrollarea中,我添加了16个chcekbox。

我想在scrollarea中循环并检查选中哪个复选框。

任何人都可以帮助我。提前谢谢!

ScrollArea和Checkbox的代码如下:

<widget class="QScrollArea" name="scrollArea">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>320</y>
     <width>271</width>
     <height>206</height>
    </rect>
   </property>
   <property name="widgetResizable">
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="scrollAreaWidgetContents">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>252</width>
      <height>420</height>
     </rect>
    </property>
    <layout class="QGridLayout" name="gridLayout">
     <item row="1" column="0">
      <widget class="QCheckBox" name="checkBox1">
       <property name="text">
        <string>1</string>
       </property>
      </widget>
     </item>
     <item row="7" column="0">
      <widget class="QCheckBox" name="checkBox7">
       <property name="text">
        <string>2</string>
       </property>
      </widget>
     </item>
     <item row="5" column="0">
      <widget class="QCheckBox" name="checkBox5">
       <property name="text">
        <string>3</string>
       </property>
      </widget>
     </item>
     <item row="2" column="0">
      <widget class="QCheckBox" name="checkBox2">
       <property name="text">
        <string>4</string>
       </property>
      </widget>
     </item>
     <item row="0" column="0">
      <widget class="QCheckBox" name="checkBox0">
       <property name="text">
        <string>5</string>
       </property>
      </widget>
     </item>
     <item row="3" column="0">
      <widget class="QCheckBox" name="checkBox3">
       <property name="text">
        <string>6</string>
       </property>
      </widget>
     </item>
     <item row="6" column="0">
      <widget class="QCheckBox" name="checkBox6">
       <property name="text">
        <string>7</string>
       </property>
      </widget>
     </item>
     <item row="4" column="0">
      <widget class="QCheckBox" name="checkBox4">
       <property name="text">
        <string>8</string>
       </property>
      </widget>
     </item>
     <item row="12" column="0">
      <widget class="QCheckBox" name="checkBox12">
       <property name="text">
        <string>9</string>
       </property>
      </widget>
     </item>
     <item row="10" column="0">
      <widget class="QCheckBox" name="checkBox10">
       <property name="text">
        <string>10</string>
       </property>
      </widget>
     </item>
     <item row="11" column="0">
      <widget class="QCheckBox" name="checkBox11">
       <property name="text">
        <string>11</string>
       </property>
      </widget>
     </item>
     <item row="9" column="0">
      <widget class="QCheckBox" name="checkBox9">
       <property name="text">
        <string>Signal Mask</string>
       </property>
      </widget>
     </item>
     <item row="8" column="0">
      <widget class="QCheckBox" name="checkBox8">
       <property name="text">
        <string>12</string>
       </property>
      </widget>
     </item>
     <item row="15" column="0">
      <widget class="QCheckBox" name="checkBox15">
       <property name="text">
        <string>13</string>
       </property>
      </widget>
     </item>
     <item row="14" column="0">
      <widget class="QCheckBox" name="checkBox14">
       <property name="text">
        <string>14</string>
       </property>
      </widget>
     </item>
     <item row="13" column="0">
      <widget class="QCheckBox" name="checkBox13">
       <property name="text">
        <string>15</string>
       </property>
      </widget>
     </item>
     <item row="16" column="0">
      <widget class="QCheckBox" name="checkBox16">
       <property name="text">
        <string>16</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>

1 个答案:

答案 0 :(得分:1)

这可以使用QObject::findChildren方法完成。

.cpp文件中属于.ui文件的示例代码如下所示:

QList<QCheckBox *> allCheckBoxes = this->findChildren<QCheckBox *>();

for (QCheckBox *checkBox: allCheckBoxes) {
   if (checkBox->isChecked()) {
      qDebug() << "CheckBox with object name " << checkBox->objectName() << " is checked";
   }
}