可以通过以下代码获取QTreeWidget中所有加入的QPushButton:
QList<QPushButton*> buttonList;
for(int i = 0; i < treeWidget->topLevelItemCount(); i++) {
QTreeWidgetItem *item = treeWidget->topLevelItem(i);
for(int j = 0; j < item->columnCount(); j++) {
QWidget *widget = treeWidget->itemWidget(item, j);
QPushButton *button = qobject_cast<QPushButton*>(widget);
if(button) {
buttonList.append(button);
}
}
}
// 使用buttonList获取所有QPushButton
这段代码首先遍历顶层项,然后在每个列中找到对应的QWidget,然后将其转换为QPushButton,如果是QPushButton就将其添加到QList中。最后就可以使用buttonList获取所有QPushButton了。