Thanks for your reply!! I've found a way, since i was stuck from 3 days and i was planning to quit qt5 and go back on c# again.
Thats how i feel the tree:
ui->treeWidget->reset();
ui->treeWidget->setColumnHidden(1, true);
ui->treeWidget->header()->close();
QSqlQuery queryCategory
("SELECT * FROM category");
QList< QPair<int, QString> > list;
while (queryCategory.next()) {
int idCategory = queryCategory.value(0).toInt();
QString catName
= queryCategory.
value(1).
toString();
//I need to save the ID to run the second query before to fill the treeWidget
list.
append(QPair<
int ,
QString >
(idCategory , catName
));
}
for(int x = 0; x < list.count(); x++ )
{
categoryItem->setText(0, list[x].second);
categoryItem
->setText
(1,
QString::number(list
[x
].
first));
//Check if we have subcategory
querySubCategory.prepare("SELECT idSubCategory, sub_Name FROM subcategory WHERE idCategory = :id");
querySubCategory.bindValue(":id", list[x].first);
querySubCategory.exec();
while(querySubCategory.next()){
int idSubCategory = querySubCategory.value(0).toInt();
QString subCatName
= querySubCategory.
value(1).
toString();
subCategoryItem->setText(0, subCatName);
subCategoryItem
->setText
(1,
QString::number(idSubCategory
));
}
}
ui->treeWidget->reset();
ui->treeWidget->setColumnHidden(1, true);
ui->treeWidget->header()->close();
QSqlQuery queryCategory("SELECT * FROM category");
QList< QPair<int, QString> > list;
while (queryCategory.next()) {
int idCategory = queryCategory.value(0).toInt();
QString catName = queryCategory.value(1).toString();
//I need to save the ID to run the second query before to fill the treeWidget
list.append(QPair< int , QString >(idCategory , catName));
}
for(int x = 0; x < list.count(); x++ )
{
QTreeWidgetItem *categoryItem = new QTreeWidgetItem(ui->treeWidget);
categoryItem->setText(0, list[x].second);
categoryItem->setText(1, QString::number(list[x].first));
//Check if we have subcategory
QSqlQuery querySubCategory;
querySubCategory.prepare("SELECT idSubCategory, sub_Name FROM subcategory WHERE idCategory = :id");
querySubCategory.bindValue(":id", list[x].first);
querySubCategory.exec();
while(querySubCategory.next()){
int idSubCategory = querySubCategory.value(0).toInt();
QString subCatName = querySubCategory.value(1).toString();
QTreeWidgetItem *subCategoryItem = new QTreeWidgetItem(categoryItem);
subCategoryItem->setText(0, subCatName);
subCategoryItem->setText(1, QString::number(idSubCategory));
}
}
ui->treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
To copy to clipboard, switch view to plain text mode
Thats how i retrieve the ID of the item, to run the query for the tableview:
void MainWindow
::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item,
int column
) {
//Check if we click a parent or a child
if(item->parent() != NULL){
QString filter
= "component.`idSubCategory` = " + item
->text
(column
+ 1);
model->setTable("component");
model->setFilter(filter);
model
->setRelation
(5,
QSqlRelation("subcategory",
"idSubCategory",
"sub_Name"));
model
->setRelation
(7,
QSqlRelation("location",
"idLocation",
"loc_Name"));
PopulateTableView(model);
}
}
void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
//Check if we click a parent or a child
if(item->parent() != NULL){
QString filter = "component.`idSubCategory` = " + item->text(column + 1);
QSqlRelationalTableModel *model = new QSqlRelationalTableModel();
model->setTable("component");
model->setFilter(filter);
model->setRelation(5, QSqlRelation("subcategory", "idSubCategory", "sub_Name"));
model->setRelation(7, QSqlRelation("location", "idLocation", "loc_Name"));
PopulateTableView(model);
}
}
To copy to clipboard, switch view to plain text mode
I'm sure that my implementation it's wrong, since you have to create custom model or item but it work for me. The problem is the documentation about this model/view is really bad for a new user that start to use qt5.
Bookmarks