Reputation: 21
I'm trying to create hierarchical tree from sql database. Tree nodes can have the same name, but unique ID. Therefore I search for a node parent by its ID, recognize its index and insert a new child row.
Also I know how QModelIndex
es are represented in QTreeView
.
Example pic
But I'm stuck in QStandardItemModel::findItems
. It searches only in QModelIndex(row, col)
, but never in QModelIndex(row, col, QModel(row,col))
.
I would appreciate any advice how to find second column in hierarchical tree model.
Here is my minimum code: main.cpp
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QTreeView>
#include <QStandardItemModel>
void createDB()
{
QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(":memory:");
if (!m_db.open()) {
return;
}
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS tags ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"parent_id INTEGER DEFAULT -1 REFERENCES tags(id),"
"title TEXT NOT NULL);");
query.exec("INSERT INTO tags(title) VALUES('item 1');");
query.exec("INSERT INTO tags(title) VALUES('item 2');");
query.exec("INSERT INTO tags(parent_id, title) VALUES(1, 'sub 1');");
query.exec("INSERT INTO tags(parent_id, title) VALUES(2, 'sub 1');");
query.exec("INSERT INTO tags(parent_id, title) VALUES(2, 'sub 2');");
query.exec("INSERT INTO tags(parent_id, title) VALUES(3, 'sub sub 1');");
query.exec("INSERT INTO tags(parent_id, title) VALUES(3, 'sub sub 2');");
}
void createTreeView() {
QTreeView *m_view = new QTreeView;
QStandardItemModel *m_model = new QStandardItemModel;
m_model->setColumnCount(2);
m_view->setModel(m_model);
QSqlQuery query("SELECT id, parent_id, title FROM tags;");
while (query.next()) {
QList<QStandardItem *> node;
node << new QStandardItem(query.value("title").toString());
node << new QStandardItem(query.value("id").toString());
if (query.value("parent_id").toInt() == -1) {
m_model->appendRow(node);
} else {
QList<QStandardItem *> items = m_model->findItems(query.value("parent_id").toString(), Qt::MatchExactly | Qt::MatchRecursive, 1);
for (QStandardItem *item : items) {
QModelIndex index = item->index().siblingAtColumn(0);
item = m_model->itemFromIndex(index);
item->appendRow(node);
}
}
}
m_view->expandAll();
m_view->resizeColumnToContents(0);
//m_view->hideColumn(1);
m_model->setHeaderData(0, Qt::Horizontal, QVariant());
m_view->show();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
createDB();
createTreeView();
return a.exec();
}
What I want to get:
> item 1
> sub 1
sub sub 1 // does not appear, because `findItems` returns an empty list.
sub sub 2 // same
> item 2
sub 1
sub 2
I have Qt 5.12.1, MSVC 2017 32bit.
Upvotes: 2
Views: 2310
Reputation: 149
You didn't use the correct "second" parameter of the function "StandardItemModel::findItems". The default scan depth of the function is 1 layer. So, if you want to seek recusively, you should use the parameter "Qt::MatchRecursive".It like:
modelname.findItems("xxxx", Qt::MatchRecursive);
Upvotes: 3