大家好,又见面了,我是你们的朋友全栈君。 #include <QtGui>
class TreeView :public QTreeView { public: TreeView(); void iterateOverItems(); QList<QStandardItem*> returnTheItems(); private: QStandardItemModel *model; };
TreeView::TreeView() : QTreeView() { model = new QStandardItemModel(4,1); QStandardItem *item1 = new QStandardItem(“first”); QStandardItem *item2 = new QStandardItem(“second”); QStandardItem *item3 = new QStandardItem(“third”); QStandardItem *item4 = new QStandardItem(“fourth”);
model->setItem(0, 0, item1); model->setItem(1, 0, item2); model->setItem(2, 0, item3); model->setItem(3, 0, item4);
QStandardItem *item5 = new QStandardItem(“fifth”); item4->appendRow(item5); setModel(model); }
QList<QStandardItem*> TreeView::returnTheItems() { return model->findItems(“*”, Qt::MatchWildcard | Qt::MatchRecursive); }
void TreeView::iterateOverItems() { QList<QStandardItem*> list = returnTheItems(); foreach (QStandardItem* item, list) { qDebug() << item->text(); } }
int main(int argc, char **argv) { QApplication app(argc, argv); TreeView view; view.iterateOverItems(); view.show(); return app.exec(); }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166394.html原文链接:https://javaforall.cn