Populate QTreeView as parent and child nodes from database












1














I have data stored in the following columns in the database



|AcName|ActCod|GroupCode|

|parent1| |1| |0|

|child1| |101| |1|

|parent2| |2| |0|

|child2| |201| |2|


I am using a QTreeView, QStandardItemModel, and QStandardItem to create this treeview however I don't know how to append the child to the parent.
I stored QStandardItem items into a QMap but how do I append child nodes to parent and parent nodes to rootNode?



the code.



standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();
QSqlQuery *itemqry = new QSqlQuery("SELECT GroupCode, AcName, ActCod from adm_ac");
while(itemqry->next()){
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);
rowItemMap.insert(groupcode, itemmap);

}
}
ui->treeView->setModel(standardModel);


the header file.



QStandardItemModel *standardModel;
QStandardItem *acName1;
QStandardItem *acName2;
QMap<int, QStandardItem*> rowItemMap;









share|improve this question
























  • How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
    – eyllanesc
    Nov 21 '18 at 18:46












  • GroupCode of child element matches the ActCod of parent element.
    – user2185284
    Nov 21 '18 at 19:00










  • Did my solution work for you? Any feedback?
    – eyllanesc
    Nov 22 '18 at 18:22
















1














I have data stored in the following columns in the database



|AcName|ActCod|GroupCode|

|parent1| |1| |0|

|child1| |101| |1|

|parent2| |2| |0|

|child2| |201| |2|


I am using a QTreeView, QStandardItemModel, and QStandardItem to create this treeview however I don't know how to append the child to the parent.
I stored QStandardItem items into a QMap but how do I append child nodes to parent and parent nodes to rootNode?



the code.



standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();
QSqlQuery *itemqry = new QSqlQuery("SELECT GroupCode, AcName, ActCod from adm_ac");
while(itemqry->next()){
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);
rowItemMap.insert(groupcode, itemmap);

}
}
ui->treeView->setModel(standardModel);


the header file.



QStandardItemModel *standardModel;
QStandardItem *acName1;
QStandardItem *acName2;
QMap<int, QStandardItem*> rowItemMap;









share|improve this question
























  • How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
    – eyllanesc
    Nov 21 '18 at 18:46












  • GroupCode of child element matches the ActCod of parent element.
    – user2185284
    Nov 21 '18 at 19:00










  • Did my solution work for you? Any feedback?
    – eyllanesc
    Nov 22 '18 at 18:22














1












1








1







I have data stored in the following columns in the database



|AcName|ActCod|GroupCode|

|parent1| |1| |0|

|child1| |101| |1|

|parent2| |2| |0|

|child2| |201| |2|


I am using a QTreeView, QStandardItemModel, and QStandardItem to create this treeview however I don't know how to append the child to the parent.
I stored QStandardItem items into a QMap but how do I append child nodes to parent and parent nodes to rootNode?



the code.



standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();
QSqlQuery *itemqry = new QSqlQuery("SELECT GroupCode, AcName, ActCod from adm_ac");
while(itemqry->next()){
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);
rowItemMap.insert(groupcode, itemmap);

}
}
ui->treeView->setModel(standardModel);


the header file.



QStandardItemModel *standardModel;
QStandardItem *acName1;
QStandardItem *acName2;
QMap<int, QStandardItem*> rowItemMap;









share|improve this question















I have data stored in the following columns in the database



|AcName|ActCod|GroupCode|

|parent1| |1| |0|

|child1| |101| |1|

|parent2| |2| |0|

|child2| |201| |2|


I am using a QTreeView, QStandardItemModel, and QStandardItem to create this treeview however I don't know how to append the child to the parent.
I stored QStandardItem items into a QMap but how do I append child nodes to parent and parent nodes to rootNode?



the code.



standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();
QSqlQuery *itemqry = new QSqlQuery("SELECT GroupCode, AcName, ActCod from adm_ac");
while(itemqry->next()){
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);
rowItemMap.insert(groupcode, itemmap);

}
}
ui->treeView->setModel(standardModel);


the header file.



QStandardItemModel *standardModel;
QStandardItem *acName1;
QStandardItem *acName2;
QMap<int, QStandardItem*> rowItemMap;






c++ qt qt5 qtreeview qstandarditemmodel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 6:04









eyllanesc

74.5k103156




74.5k103156










asked Nov 21 '18 at 18:10









user2185284user2185284

108110




108110












  • How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
    – eyllanesc
    Nov 21 '18 at 18:46












  • GroupCode of child element matches the ActCod of parent element.
    – user2185284
    Nov 21 '18 at 19:00










  • Did my solution work for you? Any feedback?
    – eyllanesc
    Nov 22 '18 at 18:22


















  • How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
    – eyllanesc
    Nov 21 '18 at 18:46












  • GroupCode of child element matches the ActCod of parent element.
    – user2185284
    Nov 21 '18 at 19:00










  • Did my solution work for you? Any feedback?
    – eyllanesc
    Nov 22 '18 at 18:22
















How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
– eyllanesc
Nov 21 '18 at 18:46






How do you identify that one row is a child of another row ?, We could assume many things with the table you show, but it is better if you indicate it clearly
– eyllanesc
Nov 21 '18 at 18:46














GroupCode of child element matches the ActCod of parent element.
– user2185284
Nov 21 '18 at 19:00




GroupCode of child element matches the ActCod of parent element.
– user2185284
Nov 21 '18 at 19:00












Did my solution work for you? Any feedback?
– eyllanesc
Nov 22 '18 at 18:22




Did my solution work for you? Any feedback?
– eyllanesc
Nov 22 '18 at 18:22












2 Answers
2






active

oldest

votes


















0














One possible is to save the ActCod in a role and then do a parent search through GroupCode using the match() method:



#include <QtWidgets>
#include <QtSql>

static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open databasen"
"Unable to establish a database connection.n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.nn"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent1", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child1", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent2", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child2", 201, 2)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("A", 10000, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("B", 10001, 201)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("C", 100000, 10000)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("D", 100001, 10001)");
return true;
}

enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};

int main(int argc, char *argv)
{
QApplication a(argc, argv);
if(!createConnection())
return -1;
QStandardItemModel model;
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
model.invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = model.match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = model.itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}
QTreeView w;
w.setModel(&model);
w.expandAll();
w.show();
return a.exec();
}


enter image description here



In your case:



// ...
enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};
// ...
standardModel = new QStandardItemModel(this);
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
standardModel->invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = standardModel->match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = standardModel->itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}

ui->treeView->setModel(standardModel);


The advantage is that we do not have to create a container like QMap, and thus we can avoid problems of accessing not allowed memory as well as duplicity of elements.





UPDATE1:



enter image description here



// ...
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("EXPENSES", 5, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("SALES", 4, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ASSETS", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CAPITAL", 3, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LIAILITIES", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CURRENT ASSETS", 102, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("FIXED ASSETS", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("INTANGIBLE FIXED ASSETS", 10102, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCUM.DEP. FIXED ASSETS", 10103, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("TANGIBLE FIXED ASSETS", 10101, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MACHINERY", 1010102, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTERS", 1010103, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LAND", 1010101, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("OFFICE EQUIPMENTS", 1010104, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MOTOR VEHICLES", 1010105, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTER SOFTWARE", 1010203, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("GOODWILL", 10102001, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("PATENTS & TRADE MARKS", 10102002, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MOTOR VEHICLES", 10103004, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- OFFICE EQUIPMENTS", 10103003, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MACHINERY", 10103001, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- COMPUTERS", 10103002, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCOUNTS RECEIVABLE", 10205, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("STOCK", 1010105, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("DEPOSITS & PREPAYMENTS", 10212, 102)");
return true;
// ...


enter image description here



UPDATE2:



I forgot to mention that in my solution I assumed that the data was ordered since in my algorithm I consider that the parent is before the children but in the general case it is not correct, so it must be ordered using: ORDER BY ActCod ASC



QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac ORDER BY ActCod ASC"); 


enter image description here






share|improve this answer























  • This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
    – user2185284
    Nov 22 '18 at 18:43










  • @user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
    – eyllanesc
    Nov 22 '18 at 18:48










  • @user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
    – eyllanesc
    Nov 22 '18 at 18:51










  • @user2185284 I just tested your data and it works correctly as my update shows
    – eyllanesc
    Nov 22 '18 at 19:13










  • @user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
    – eyllanesc
    Nov 22 '18 at 19:31



















0














Here is an other way to build your tree. This code take care also the case that child nodes were loaded before its parent. The tree will be built in two pass :




  1. Load all items to the map

  2. Build the tree from the map


EDIT: We use setData() function instead of setProperty since QStardardItem is not QObject derived.



    enum GroupCodeRoleEnum{
GroupCodeRole = Qt::UserRole + 1001,
};

while(itemqry->next())
{
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);

itemMap->setData(groupcode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
rowItemMap.insert(ActCod, itemmap) );

} //End of while -- every node were now loaded.

//build the tree
rowItemMap.insert (0, rootNode ) ;

foreach( QStandardItem * p, rowItemMap.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = rowItemMap.find( groupCode );

if( it != rowItemMap.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);


TEST CODE:



        QStandardItemModel *standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();

//Simulate your while() loop.
addItem(1, 0, "parent 1");
addItem(101, 1, "child 1");
addItem(2, 0, "parent 2");
addItem(201, 2, "child 2");

//childs were loaded before parents

addItem(301, 3, "child 3");
addItem(401, 4, "child 4");
addItem(501, 5, "child 5");

addItem(3, 2, "parent 3");
addItem(4, 2, "parent 4");
addItem(5, 2, "parent 5");

//build the tree
m_map.insert (0, rootNode ) ;

foreach( QStandardItem * p, m_map.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = m_map.find( groupCode );

if( it != m_map.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);
}

void MainWindow::addItem(int Act, int nGroupCode, QString szName)
{
QStandardItem *itemmap = new QStandardItem( szName );

itemmap->setData(nGroupCode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
m_map.insert(Act, itemmap);

}


The tree was built :



enter image description here






share|improve this answer























  • class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
    – user2185284
    Nov 22 '18 at 18:00










  • @user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
    – tunglt
    Nov 22 '18 at 18:26










  • With this code the treeView remains empty
    – user2185284
    Nov 23 '18 at 11:02










  • @user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
    – tunglt
    Nov 23 '18 at 11:22










  • root's groupcode is empty. so it is zero. How does the test code still work?
    – user2185284
    Nov 24 '18 at 14:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418180%2fpopulate-qtreeview-as-parent-and-child-nodes-from-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














One possible is to save the ActCod in a role and then do a parent search through GroupCode using the match() method:



#include <QtWidgets>
#include <QtSql>

static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open databasen"
"Unable to establish a database connection.n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.nn"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent1", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child1", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent2", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child2", 201, 2)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("A", 10000, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("B", 10001, 201)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("C", 100000, 10000)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("D", 100001, 10001)");
return true;
}

enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};

int main(int argc, char *argv)
{
QApplication a(argc, argv);
if(!createConnection())
return -1;
QStandardItemModel model;
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
model.invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = model.match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = model.itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}
QTreeView w;
w.setModel(&model);
w.expandAll();
w.show();
return a.exec();
}


enter image description here



In your case:



// ...
enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};
// ...
standardModel = new QStandardItemModel(this);
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
standardModel->invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = standardModel->match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = standardModel->itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}

ui->treeView->setModel(standardModel);


The advantage is that we do not have to create a container like QMap, and thus we can avoid problems of accessing not allowed memory as well as duplicity of elements.





UPDATE1:



enter image description here



// ...
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("EXPENSES", 5, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("SALES", 4, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ASSETS", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CAPITAL", 3, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LIAILITIES", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CURRENT ASSETS", 102, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("FIXED ASSETS", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("INTANGIBLE FIXED ASSETS", 10102, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCUM.DEP. FIXED ASSETS", 10103, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("TANGIBLE FIXED ASSETS", 10101, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MACHINERY", 1010102, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTERS", 1010103, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LAND", 1010101, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("OFFICE EQUIPMENTS", 1010104, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MOTOR VEHICLES", 1010105, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTER SOFTWARE", 1010203, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("GOODWILL", 10102001, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("PATENTS & TRADE MARKS", 10102002, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MOTOR VEHICLES", 10103004, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- OFFICE EQUIPMENTS", 10103003, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MACHINERY", 10103001, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- COMPUTERS", 10103002, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCOUNTS RECEIVABLE", 10205, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("STOCK", 1010105, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("DEPOSITS & PREPAYMENTS", 10212, 102)");
return true;
// ...


enter image description here



UPDATE2:



I forgot to mention that in my solution I assumed that the data was ordered since in my algorithm I consider that the parent is before the children but in the general case it is not correct, so it must be ordered using: ORDER BY ActCod ASC



QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac ORDER BY ActCod ASC"); 


enter image description here






share|improve this answer























  • This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
    – user2185284
    Nov 22 '18 at 18:43










  • @user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
    – eyllanesc
    Nov 22 '18 at 18:48










  • @user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
    – eyllanesc
    Nov 22 '18 at 18:51










  • @user2185284 I just tested your data and it works correctly as my update shows
    – eyllanesc
    Nov 22 '18 at 19:13










  • @user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
    – eyllanesc
    Nov 22 '18 at 19:31
















0














One possible is to save the ActCod in a role and then do a parent search through GroupCode using the match() method:



#include <QtWidgets>
#include <QtSql>

static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open databasen"
"Unable to establish a database connection.n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.nn"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent1", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child1", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent2", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child2", 201, 2)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("A", 10000, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("B", 10001, 201)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("C", 100000, 10000)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("D", 100001, 10001)");
return true;
}

enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};

int main(int argc, char *argv)
{
QApplication a(argc, argv);
if(!createConnection())
return -1;
QStandardItemModel model;
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
model.invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = model.match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = model.itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}
QTreeView w;
w.setModel(&model);
w.expandAll();
w.show();
return a.exec();
}


enter image description here



In your case:



// ...
enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};
// ...
standardModel = new QStandardItemModel(this);
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
standardModel->invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = standardModel->match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = standardModel->itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}

ui->treeView->setModel(standardModel);


The advantage is that we do not have to create a container like QMap, and thus we can avoid problems of accessing not allowed memory as well as duplicity of elements.





UPDATE1:



enter image description here



// ...
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("EXPENSES", 5, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("SALES", 4, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ASSETS", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CAPITAL", 3, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LIAILITIES", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CURRENT ASSETS", 102, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("FIXED ASSETS", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("INTANGIBLE FIXED ASSETS", 10102, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCUM.DEP. FIXED ASSETS", 10103, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("TANGIBLE FIXED ASSETS", 10101, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MACHINERY", 1010102, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTERS", 1010103, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LAND", 1010101, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("OFFICE EQUIPMENTS", 1010104, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MOTOR VEHICLES", 1010105, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTER SOFTWARE", 1010203, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("GOODWILL", 10102001, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("PATENTS & TRADE MARKS", 10102002, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MOTOR VEHICLES", 10103004, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- OFFICE EQUIPMENTS", 10103003, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MACHINERY", 10103001, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- COMPUTERS", 10103002, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCOUNTS RECEIVABLE", 10205, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("STOCK", 1010105, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("DEPOSITS & PREPAYMENTS", 10212, 102)");
return true;
// ...


enter image description here



UPDATE2:



I forgot to mention that in my solution I assumed that the data was ordered since in my algorithm I consider that the parent is before the children but in the general case it is not correct, so it must be ordered using: ORDER BY ActCod ASC



QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac ORDER BY ActCod ASC"); 


enter image description here






share|improve this answer























  • This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
    – user2185284
    Nov 22 '18 at 18:43










  • @user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
    – eyllanesc
    Nov 22 '18 at 18:48










  • @user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
    – eyllanesc
    Nov 22 '18 at 18:51










  • @user2185284 I just tested your data and it works correctly as my update shows
    – eyllanesc
    Nov 22 '18 at 19:13










  • @user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
    – eyllanesc
    Nov 22 '18 at 19:31














0












0








0






One possible is to save the ActCod in a role and then do a parent search through GroupCode using the match() method:



#include <QtWidgets>
#include <QtSql>

static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open databasen"
"Unable to establish a database connection.n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.nn"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent1", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child1", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent2", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child2", 201, 2)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("A", 10000, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("B", 10001, 201)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("C", 100000, 10000)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("D", 100001, 10001)");
return true;
}

enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};

int main(int argc, char *argv)
{
QApplication a(argc, argv);
if(!createConnection())
return -1;
QStandardItemModel model;
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
model.invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = model.match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = model.itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}
QTreeView w;
w.setModel(&model);
w.expandAll();
w.show();
return a.exec();
}


enter image description here



In your case:



// ...
enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};
// ...
standardModel = new QStandardItemModel(this);
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
standardModel->invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = standardModel->match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = standardModel->itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}

ui->treeView->setModel(standardModel);


The advantage is that we do not have to create a container like QMap, and thus we can avoid problems of accessing not allowed memory as well as duplicity of elements.





UPDATE1:



enter image description here



// ...
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("EXPENSES", 5, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("SALES", 4, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ASSETS", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CAPITAL", 3, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LIAILITIES", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CURRENT ASSETS", 102, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("FIXED ASSETS", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("INTANGIBLE FIXED ASSETS", 10102, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCUM.DEP. FIXED ASSETS", 10103, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("TANGIBLE FIXED ASSETS", 10101, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MACHINERY", 1010102, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTERS", 1010103, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LAND", 1010101, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("OFFICE EQUIPMENTS", 1010104, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MOTOR VEHICLES", 1010105, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTER SOFTWARE", 1010203, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("GOODWILL", 10102001, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("PATENTS & TRADE MARKS", 10102002, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MOTOR VEHICLES", 10103004, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- OFFICE EQUIPMENTS", 10103003, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MACHINERY", 10103001, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- COMPUTERS", 10103002, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCOUNTS RECEIVABLE", 10205, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("STOCK", 1010105, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("DEPOSITS & PREPAYMENTS", 10212, 102)");
return true;
// ...


enter image description here



UPDATE2:



I forgot to mention that in my solution I assumed that the data was ordered since in my algorithm I consider that the parent is before the children but in the general case it is not correct, so it must be ordered using: ORDER BY ActCod ASC



QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac ORDER BY ActCod ASC"); 


enter image description here






share|improve this answer














One possible is to save the ActCod in a role and then do a parent search through GroupCode using the match() method:



#include <QtWidgets>
#include <QtSql>

static bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open databasen"
"Unable to establish a database connection.n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.nn"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent1", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child1", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("parent2", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("child2", 201, 2)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("A", 10000, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("B", 10001, 201)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("C", 100000, 10000)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("D", 100001, 10001)");
return true;
}

enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};

int main(int argc, char *argv)
{
QApplication a(argc, argv);
if(!createConnection())
return -1;
QStandardItemModel model;
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
model.invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = model.match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = model.itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}
QTreeView w;
w.setModel(&model);
w.expandAll();
w.show();
return a.exec();
}


enter image description here



In your case:



// ...
enum RelationRoles{
CodeRole = Qt::UserRole + 1000,
};
// ...
standardModel = new QStandardItemModel(this);
QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac");
const QSqlRecord rec = query.record();
while (query.next()) {
QString AcName = query.value(rec.indexOf("AcName")).toString();
int GroupCode = query.value(rec.indexOf("GroupCode")).toInt();
int ActCod = query.value(rec.indexOf("ActCod")).toInt();
QStandardItem *it = new QStandardItem(AcName);
it->setData(ActCod, RelationRoles::CodeRole);
if(GroupCode == 0)
standardModel->invisibleRootItem()->appendRow(it);
else{
QModelIndexList ixs = standardModel->match(model.index(0, 0),
RelationRoles::CodeRole,
GroupCode,
1,
Qt::MatchExactly| Qt::MatchRecursive);
if(ixs.size() > 0){
QStandardItem *parent = standardModel->itemFromIndex(ixs.first());
parent->appendRow(it);
}
}
}

ui->treeView->setModel(standardModel);


The advantage is that we do not have to create a container like QMap, and thus we can avoid problems of accessing not allowed memory as well as duplicity of elements.





UPDATE1:



enter image description here



// ...
QSqlQuery query;
if(!query.exec("CREATE TABLE adm_ac("
"AcName TEXT,"
"ActCod INTEGER,"
"GroupCode INTEGER"
")"))
qDebug()<<query.lastError().text();
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("EXPENSES", 5, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("SALES", 4, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ASSETS", 1, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CAPITAL", 3, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LIAILITIES", 2, 0)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("CURRENT ASSETS", 102, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("FIXED ASSETS", 101, 1)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("INTANGIBLE FIXED ASSETS", 10102, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCUM.DEP. FIXED ASSETS", 10103, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("TANGIBLE FIXED ASSETS", 10101, 101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MACHINERY", 1010102, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTERS", 1010103, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("LAND", 1010101, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("OFFICE EQUIPMENTS", 1010104, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("MOTOR VEHICLES", 1010105, 10101)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("COMPUTER SOFTWARE", 1010203, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("GOODWILL", 10102001, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("PATENTS & TRADE MARKS", 10102002, 10102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MOTOR VEHICLES", 10103004, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- OFFICE EQUIPMENTS", 10103003, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- MACHINERY", 10103001, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACC.DEP- COMPUTERS", 10103002, 10103)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("ACCOUNTS RECEIVABLE", 10205, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("STOCK", 1010105, 102)");
query.exec("insert into adm_ac(AcName, ActCod, GroupCode) values("DEPOSITS & PREPAYMENTS", 10212, 102)");
return true;
// ...


enter image description here



UPDATE2:



I forgot to mention that in my solution I assumed that the data was ordered since in my algorithm I consider that the parent is before the children but in the general case it is not correct, so it must be ordered using: ORDER BY ActCod ASC



QSqlQuery query("SELECT GroupCode, AcName, ActCod from adm_ac ORDER BY ActCod ASC"); 


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 5:53

























answered Nov 21 '18 at 20:40









eyllanesceyllanesc

74.5k103156




74.5k103156












  • This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
    – user2185284
    Nov 22 '18 at 18:43










  • @user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
    – eyllanesc
    Nov 22 '18 at 18:48










  • @user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
    – eyllanesc
    Nov 22 '18 at 18:51










  • @user2185284 I just tested your data and it works correctly as my update shows
    – eyllanesc
    Nov 22 '18 at 19:13










  • @user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
    – eyllanesc
    Nov 22 '18 at 19:31


















  • This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
    – user2185284
    Nov 22 '18 at 18:43










  • @user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
    – eyllanesc
    Nov 22 '18 at 18:48










  • @user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
    – eyllanesc
    Nov 22 '18 at 18:51










  • @user2185284 I just tested your data and it works correctly as my update shows
    – eyllanesc
    Nov 22 '18 at 19:13










  • @user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
    – eyllanesc
    Nov 22 '18 at 19:31
















This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
– user2185284
Nov 22 '18 at 18:43




This works but it does not go for deeper menus. e.g. rootNode only has parent items which is correct, but every parrent can have multiple child, and each child can also have multiple children of it's own. Your code somewhat works but it also misplaces the nodes under other nodes which do not have any parent-child relationship that is matching the GroupCode to ActCod see this image: link
– user2185284
Nov 22 '18 at 18:43












@user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
– eyllanesc
Nov 22 '18 at 18:48




@user2185284 Strange, just in my example I implemented a similar example to show that my solution works for several levels, if you are using sqlite you could share the .db, or if you are using another database you could share a .sql that contains the data. Are you using my last code?
– eyllanesc
Nov 22 '18 at 18:48












@user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
– eyllanesc
Nov 22 '18 at 18:51




@user2185284 I see that there are Group Code empty but in the table that you show in your question are 0, do not you think that would bring problems?
– eyllanesc
Nov 22 '18 at 18:51












@user2185284 I just tested your data and it works correctly as my update shows
– eyllanesc
Nov 22 '18 at 19:13




@user2185284 I just tested your data and it works correctly as my update shows
– eyllanesc
Nov 22 '18 at 19:13












@user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
– eyllanesc
Nov 22 '18 at 19:31




@user2185284 If you have used my second code I just realized that I had errors, please try it again :-)
– eyllanesc
Nov 22 '18 at 19:31













0














Here is an other way to build your tree. This code take care also the case that child nodes were loaded before its parent. The tree will be built in two pass :




  1. Load all items to the map

  2. Build the tree from the map


EDIT: We use setData() function instead of setProperty since QStardardItem is not QObject derived.



    enum GroupCodeRoleEnum{
GroupCodeRole = Qt::UserRole + 1001,
};

while(itemqry->next())
{
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);

itemMap->setData(groupcode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
rowItemMap.insert(ActCod, itemmap) );

} //End of while -- every node were now loaded.

//build the tree
rowItemMap.insert (0, rootNode ) ;

foreach( QStandardItem * p, rowItemMap.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = rowItemMap.find( groupCode );

if( it != rowItemMap.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);


TEST CODE:



        QStandardItemModel *standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();

//Simulate your while() loop.
addItem(1, 0, "parent 1");
addItem(101, 1, "child 1");
addItem(2, 0, "parent 2");
addItem(201, 2, "child 2");

//childs were loaded before parents

addItem(301, 3, "child 3");
addItem(401, 4, "child 4");
addItem(501, 5, "child 5");

addItem(3, 2, "parent 3");
addItem(4, 2, "parent 4");
addItem(5, 2, "parent 5");

//build the tree
m_map.insert (0, rootNode ) ;

foreach( QStandardItem * p, m_map.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = m_map.find( groupCode );

if( it != m_map.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);
}

void MainWindow::addItem(int Act, int nGroupCode, QString szName)
{
QStandardItem *itemmap = new QStandardItem( szName );

itemmap->setData(nGroupCode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
m_map.insert(Act, itemmap);

}


The tree was built :



enter image description here






share|improve this answer























  • class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
    – user2185284
    Nov 22 '18 at 18:00










  • @user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
    – tunglt
    Nov 22 '18 at 18:26










  • With this code the treeView remains empty
    – user2185284
    Nov 23 '18 at 11:02










  • @user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
    – tunglt
    Nov 23 '18 at 11:22










  • root's groupcode is empty. so it is zero. How does the test code still work?
    – user2185284
    Nov 24 '18 at 14:09
















0














Here is an other way to build your tree. This code take care also the case that child nodes were loaded before its parent. The tree will be built in two pass :




  1. Load all items to the map

  2. Build the tree from the map


EDIT: We use setData() function instead of setProperty since QStardardItem is not QObject derived.



    enum GroupCodeRoleEnum{
GroupCodeRole = Qt::UserRole + 1001,
};

while(itemqry->next())
{
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);

itemMap->setData(groupcode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
rowItemMap.insert(ActCod, itemmap) );

} //End of while -- every node were now loaded.

//build the tree
rowItemMap.insert (0, rootNode ) ;

foreach( QStandardItem * p, rowItemMap.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = rowItemMap.find( groupCode );

if( it != rowItemMap.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);


TEST CODE:



        QStandardItemModel *standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();

//Simulate your while() loop.
addItem(1, 0, "parent 1");
addItem(101, 1, "child 1");
addItem(2, 0, "parent 2");
addItem(201, 2, "child 2");

//childs were loaded before parents

addItem(301, 3, "child 3");
addItem(401, 4, "child 4");
addItem(501, 5, "child 5");

addItem(3, 2, "parent 3");
addItem(4, 2, "parent 4");
addItem(5, 2, "parent 5");

//build the tree
m_map.insert (0, rootNode ) ;

foreach( QStandardItem * p, m_map.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = m_map.find( groupCode );

if( it != m_map.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);
}

void MainWindow::addItem(int Act, int nGroupCode, QString szName)
{
QStandardItem *itemmap = new QStandardItem( szName );

itemmap->setData(nGroupCode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
m_map.insert(Act, itemmap);

}


The tree was built :



enter image description here






share|improve this answer























  • class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
    – user2185284
    Nov 22 '18 at 18:00










  • @user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
    – tunglt
    Nov 22 '18 at 18:26










  • With this code the treeView remains empty
    – user2185284
    Nov 23 '18 at 11:02










  • @user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
    – tunglt
    Nov 23 '18 at 11:22










  • root's groupcode is empty. so it is zero. How does the test code still work?
    – user2185284
    Nov 24 '18 at 14:09














0












0








0






Here is an other way to build your tree. This code take care also the case that child nodes were loaded before its parent. The tree will be built in two pass :




  1. Load all items to the map

  2. Build the tree from the map


EDIT: We use setData() function instead of setProperty since QStardardItem is not QObject derived.



    enum GroupCodeRoleEnum{
GroupCodeRole = Qt::UserRole + 1001,
};

while(itemqry->next())
{
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);

itemMap->setData(groupcode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
rowItemMap.insert(ActCod, itemmap) );

} //End of while -- every node were now loaded.

//build the tree
rowItemMap.insert (0, rootNode ) ;

foreach( QStandardItem * p, rowItemMap.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = rowItemMap.find( groupCode );

if( it != rowItemMap.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);


TEST CODE:



        QStandardItemModel *standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();

//Simulate your while() loop.
addItem(1, 0, "parent 1");
addItem(101, 1, "child 1");
addItem(2, 0, "parent 2");
addItem(201, 2, "child 2");

//childs were loaded before parents

addItem(301, 3, "child 3");
addItem(401, 4, "child 4");
addItem(501, 5, "child 5");

addItem(3, 2, "parent 3");
addItem(4, 2, "parent 4");
addItem(5, 2, "parent 5");

//build the tree
m_map.insert (0, rootNode ) ;

foreach( QStandardItem * p, m_map.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = m_map.find( groupCode );

if( it != m_map.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);
}

void MainWindow::addItem(int Act, int nGroupCode, QString szName)
{
QStandardItem *itemmap = new QStandardItem( szName );

itemmap->setData(nGroupCode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
m_map.insert(Act, itemmap);

}


The tree was built :



enter image description here






share|improve this answer














Here is an other way to build your tree. This code take care also the case that child nodes were loaded before its parent. The tree will be built in two pass :




  1. Load all items to the map

  2. Build the tree from the map


EDIT: We use setData() function instead of setProperty since QStardardItem is not QObject derived.



    enum GroupCodeRoleEnum{
GroupCodeRole = Qt::UserRole + 1001,
};

while(itemqry->next())
{
int groupcode =itemqry->value(0).toInt();
QString acname = itemqry->value(1).toString();
int ActCod = itemqry->value(2).toInt();
QStandardItem *itemmap = new QStandardItem(acname);

itemMap->setData(groupcode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
rowItemMap.insert(ActCod, itemmap) );

} //End of while -- every node were now loaded.

//build the tree
rowItemMap.insert (0, rootNode ) ;

foreach( QStandardItem * p, rowItemMap.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = rowItemMap.find( groupCode );

if( it != rowItemMap.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);


TEST CODE:



        QStandardItemModel *standardModel = new QStandardItemModel(this);
QStandardItem *rootNode = standardModel->invisibleRootItem();

//Simulate your while() loop.
addItem(1, 0, "parent 1");
addItem(101, 1, "child 1");
addItem(2, 0, "parent 2");
addItem(201, 2, "child 2");

//childs were loaded before parents

addItem(301, 3, "child 3");
addItem(401, 4, "child 4");
addItem(501, 5, "child 5");

addItem(3, 2, "parent 3");
addItem(4, 2, "parent 4");
addItem(5, 2, "parent 5");

//build the tree
m_map.insert (0, rootNode ) ;

foreach( QStandardItem * p, m_map.values() ){
int groupCode = p->data( GroupCodeRole ).toInt();

//find the parent from the map
if( p != rootNode ){

QMap<int, QStandardItem* >::iterator it = m_map.find( groupCode );

if( it != m_map.end() ){

QStandardItem* pParent = it.value();
pParent->appendRow( p );
}else {
qDebug() << "Parent not exist for groupCode" << groupCode;
}

}//nomal node

} //foreach

ui->treeView->setModel(standardModel);
}

void MainWindow::addItem(int Act, int nGroupCode, QString szName)
{
QStandardItem *itemmap = new QStandardItem( szName );

itemmap->setData(nGroupCode, GroupCodeRole );

//map of ActCod to itemmap, not GroupCode
m_map.insert(Act, itemmap);

}


The tree was built :



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 15:55

























answered Nov 21 '18 at 21:04









tunglttunglt

60928




60928












  • class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
    – user2185284
    Nov 22 '18 at 18:00










  • @user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
    – tunglt
    Nov 22 '18 at 18:26










  • With this code the treeView remains empty
    – user2185284
    Nov 23 '18 at 11:02










  • @user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
    – tunglt
    Nov 23 '18 at 11:22










  • root's groupcode is empty. so it is zero. How does the test code still work?
    – user2185284
    Nov 24 '18 at 14:09


















  • class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
    – user2185284
    Nov 22 '18 at 18:00










  • @user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
    – tunglt
    Nov 22 '18 at 18:26










  • With this code the treeView remains empty
    – user2185284
    Nov 23 '18 at 11:02










  • @user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
    – tunglt
    Nov 23 '18 at 11:22










  • root's groupcode is empty. so it is zero. How does the test code still work?
    – user2185284
    Nov 24 '18 at 14:09
















class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
– user2185284
Nov 22 '18 at 18:00




class QStandardItem has no member named setProperty, Property. Variable pNode is also not declared, did you mean to use the variable p?
– user2185284
Nov 22 '18 at 18:00












@user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
– tunglt
Nov 22 '18 at 18:26




@user2185284: yes, pNode mean p, QStandardItem is not QObject derived so setProperty does not work. I modified my answer to correct it, please give it a try.
– tunglt
Nov 22 '18 at 18:26












With this code the treeView remains empty
– user2185284
Nov 23 '18 at 11:02




With this code the treeView remains empty
– user2185284
Nov 23 '18 at 11:02












@user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
– tunglt
Nov 23 '18 at 11:22




@user2185284: the root's groupCode is 0 as described in your question ? or my setData() does not work :(
– tunglt
Nov 23 '18 at 11:22












root's groupcode is empty. so it is zero. How does the test code still work?
– user2185284
Nov 24 '18 at 14:09




root's groupcode is empty. so it is zero. How does the test code still work?
– user2185284
Nov 24 '18 at 14:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418180%2fpopulate-qtreeview-as-parent-and-child-nodes-from-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python