2017-01-10 4 views
0

Я добавил виджет QScrollArea в форме QDialog.Доступ к детям из QScrollArea

В scrollarea я добавил 16 chcekboxes.

Я хочу зациклиться внутри прокрутки и проверить, какие флажки отмечены.

Может кто-нибудь, пожалуйста, помогите мне с этим. Заранее спасибо!

Код для 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

Это может быть достигнуто с помощью метода 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"; 
    } 
} 
Смежные вопросы