[SOLVED] Search a QTreeView or the tree model
-
here you have how I'm creating my own tree
@QSqlQuery q;
QStandardItemModel *myModel;
q = db->atpSelect("SELECT * FROM group_tbl");
myModel->clear();
myModel->setColumnCount(3);
myModel->setHeaderData(0, Qt::Horizontal,"Name");
myModel->setHeaderData(1, Qt::Horizontal,"ID");
myModel->setHeaderData(2, Qt::Horizontal,"Parent");QStandardItem *parent;
while(q.next()) {int id = q.value(0).toInt();
QString name = q.value(1).toString();
int parentId = q.value(2).toInt();QStandardItem *pId = new QStandardItem(QString::number(id));
QStandardItem *pName = new QStandardItem(name);
QStandardItem *pParentId = new QStandardItem(QString::number(parentId));QList<QStandardItem *> row;
row.append(pName);
row.append(pId);
row.append(pParentId);rowItemMap.insert(id, pName);
if (parentId == 0 && id != 0) {
myModel->appendRow(row);
} else {
parent = rowItemMap[parentId];
parent->appendRow(row);
}
}@
now the list I'm apply it to a combobox
@
myComboTreeview->setModel(myModel);
myComboTreeview->hideColumn(1);
myComboTreeview->hideColumn(2);
myComboTreeview->setHeaderHidden(true);mycombo->setView(myComboTreeview);
mycombo->setModel(myModel);
mycombo->setMaxVisibleItems(10);
mycombo->setCurrentIndex(-1);
mycombo->setModelColumn(0);
@now i have a lineEdit where the user imput the id or the group(name)
what i want to do is after the user is entering the id, or the text, i use the connect when user chage the edited text in the lineEdit(QString arg)
and to find that id or name into the combobox (tree/or model) and to set the current index in the combobox.
to search in the comboboc/treeview/model for the item that the user want to fing.
keep in mind that i'v alreadi tried this and is givving be ok is is the main parrents so if the parents = 0 then this is working but else no
@
QModelIndexList Items = comboBoxGrupPlace->model()->match(
comboBoxGrupPlace->model()->index(0, 1),
Qt::DisplayRole,
QVariant::fromValue(place),
3,
Qt::MatchRecursive);
qDebug() << "new place" << Items << place << tableModelGroup->findItems(QString::number(place),Qt::MatchRecursive|Qt::MatchExactly, 1);
@
any help will be highly apreciated..
thanks -
Ok.. I've done it myself after a lot of debugging and searching.
here is how@
//place
QModelIndex myIndex, pIndex;
myIndex = findComboIndex(place,tableModelGroup->indexFromItem(tableModelGroup->invisibleRootItem()));
pIndex = myIndex.parent();
comboBoxGrupPlace->setRootModelIndex(pIndex);
comboBoxGrupPlace->setCurrentIndex(myIndex.row());
@
and ofcourse the most important the iterator function
@
QModelIndex Module_quotes_detail::findComboIndex(int id, QModelIndex parent){
int rows = tableModelGroup->rowCount(parent);
qDebug() << rows;
for (int var = 0; var < rows; ++var) {
int tmpId = tableModelGroup->data(tableModelGroup->index(var,1,parent)).toInt();
if (tmpId == id){
qDebug() << " return1: " <<tmpId <<tableModelGroup->index(var,1,parent);
QModelIndex tmpParent = tableModelGroup->index(var,0,parent);
return tmpParent;
} else {
qDebug()<< " else ";
QModelIndex tmpParent = tableModelGroup->index(var,0,parent);
if(tableModelGroup->hasChildren(tmpParent)){
qDebug() <<" are ";
return findComboIndex(id,tmpParent);
} else {
qDebug()<< " nu are ";
}
}
}
}
@