2017-01-10 61 views
0

我已經在QDialog窗體中添加了QScrollArea小部件。從QScrollArea訪問子項

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

我想在循環內滾動並檢查選中哪個複選框。

任何人都可以幫助我這個。提前致謝!

爲ScrollArea和複選框的代碼是這樣的:

<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"; 
    } 
}