首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从QScrollArea访问子对象

从QScrollArea访问子对象
EN

Stack Overflow用户
提问于 2017-01-10 18:16:45
回答 1查看 408关注 0票数 0

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

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

我想要在scrollarea中循环,并检查选中了哪些复选框。

有没有人能帮我。提前感谢!

ScrollArea和Checkbox的代码如下:

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-10 18:48:13

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

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

代码语言:javascript
运行
复制
QList<QCheckBox *> allCheckBoxes = this->findChildren<QCheckBox *>();

for (QCheckBox *checkBox: allCheckBoxes) {
   if (checkBox->isChecked()) {
      qDebug() << "CheckBox with object name " << checkBox->objectName() << " is checked";
   }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41566391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档