在开发向上显示的QCombox时,要用到QStandardItemModel and Qtreeview
QStandardItemModel的好处能设置数据、text, icon, tooltip
可Qtreeview的Mode接口是 ( QAbstractItemModel * )
下面从:
http://www.qtsoftware.com/developer/faqs/faq.2007-05-04.0389488856找到了一例,刚好能解决我的问题,现转载
#include
class TreeView :public QTreeView
{
public:
TreeView();
void iterateOverItems();
QList 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 TreeView::returnTheItems()
{
return model->findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
}
void TreeView::iterateOverItems()
{
QList 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();
}
阅读(3136) | 评论(0) | 转发(0) |