Results 1 to 4 of 4

Thread: Stuck with tree view/widget

  1. #1
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Stuck with tree view/widget

    Hi guys, it's the first time that i program with QT5 and mysql database also. Before to ask help i will show you the small database that i have to use and the gui of my application:

    DB: http://s33.postimg.org/qqht3dc6n/Table.png
    GUI: http://s33.postimg.org/cjtc013hr/gui.jpg

    1) How can i show in a TreeView/or Widget the category table (without the ID), and if the user click on the plus to expand each parent the child will be the subcategory table (also without ID)?
    2) Is it possible for example to retrieve the categoryID when the user click on one element in the tree? I will use the id to run query and display the proper data in the table.

    I just want to say that the tree and the table will be read-only, to update or add something in the database i will select a row in the table and press a button to add, remove or update.

    I hope someone could help me, it's 3 days that i'm stuck with this, and i didn't find any useful information for the qt website or google.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Stuck with tree view/widget

    Quote Originally Posted by WarOfDevil View Post
    1) How can i show in a TreeView/or Widget the category table (without the ID), and if the user click on the plus to expand each parent the child will be the subcategory table (also without ID)?
    You will either have to use a QStandardItemModel and fill it with data from SQL queries or derive from QAbstractItemModel and implement a tree model yourself, that can do the queries internally.

    Since the latter can be a bit frustrating for someone new to developing models, my suggestion would be to go for the other approach for now.

    Quote Originally Posted by WarOfDevil View Post
    2) Is it possible for example to retrieve the categoryID when the user click on one element in the tree? I will use the id to run query and display the proper data in the table.
    Yes, a model can store multiple data facets, so called roles, per entry. I.e. it can have a string to show in the view and the ID to retrieve in code.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Stuck with tree view/widget

    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:

    Qt Code:
    1. ui->treeWidget->reset();
    2. ui->treeWidget->setColumnHidden(1, true);
    3. ui->treeWidget->header()->close();
    4.  
    5. QSqlQuery queryCategory("SELECT * FROM category");
    6.  
    7. QList< QPair<int, QString> > list;
    8.  
    9. while (queryCategory.next()) {
    10.  
    11. int idCategory = queryCategory.value(0).toInt();
    12. QString catName = queryCategory.value(1).toString();
    13.  
    14. //I need to save the ID to run the second query before to fill the treeWidget
    15. list.append(QPair< int , QString >(idCategory , catName));
    16. }
    17.  
    18. for(int x = 0; x < list.count(); x++ )
    19. {
    20. QTreeWidgetItem *categoryItem = new QTreeWidgetItem(ui->treeWidget);
    21. categoryItem->setText(0, list[x].second);
    22. categoryItem->setText(1, QString::number(list[x].first));
    23.  
    24. //Check if we have subcategory
    25. QSqlQuery querySubCategory;
    26. querySubCategory.prepare("SELECT idSubCategory, sub_Name FROM subcategory WHERE idCategory = :id");
    27. querySubCategory.bindValue(":id", list[x].first);
    28. querySubCategory.exec();
    29.  
    30. while(querySubCategory.next()){
    31.  
    32. int idSubCategory = querySubCategory.value(0).toInt();
    33. QString subCatName = querySubCategory.value(1).toString();
    34.  
    35. QTreeWidgetItem *subCategoryItem = new QTreeWidgetItem(categoryItem);
    36. subCategoryItem->setText(0, subCatName);
    37. subCategoryItem->setText(1, QString::number(idSubCategory));
    38. }
    39.  
    40. }
    41.  
    42. 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:

    Qt Code:
    1. void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
    2. {
    3. //Check if we click a parent or a child
    4. if(item->parent() != NULL){
    5.  
    6. QString filter = "component.`idSubCategory` = " + item->text(column + 1);
    7.  
    8. model->setTable("component");
    9. model->setFilter(filter);
    10. model->setRelation(5, QSqlRelation("subcategory", "idSubCategory", "sub_Name"));
    11. model->setRelation(7, QSqlRelation("location", "idLocation", "loc_Name"));
    12.  
    13. PopulateTableView(model);
    14. }
    15.  
    16. }
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Stuck with tree view/widget

    Quote Originally Posted by WarOfDevil View Post
    I'm sure that my implementation it's wrong, since you have to create custom model or item but it work for me.
    No, that is actually fine, I just forgot about using QTreeWidget instead of QTreeView with a separate model.

    What you can do, however, is to store the category integer as an integer instead of having to convert to/from string and using a "hidden" column, by using the QTreeWidgetItem's data/setData:

    Storing
    Qt Code:
    1. QTreeWidgetItem *categoryItem = new QTreeWidgetItem(ui->treeWidget);
    2. categoryItem->setText(0, list[x].second);
    3. categoryItem->setData(0, Qt::UserRole, list[x].first);
    To copy to clipboard, switch view to plain text mode 

    Retrieval
    Qt Code:
    1. int category = item->data(0, Qt::UserRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    Btw, in your slot you might not want to create a new model instance every time, otherwise you also need a way to delete the previously created one or you are leaking it.
    Better keep the table view model in a member variable and only adjust the filter as needed.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 2nd June 2016, 08:42
  2. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 1
    Last Post: 6th June 2015, 16:26
  3. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 6
    Last Post: 18th May 2015, 07:18
  4. table view and tree view
    By MKSPulok in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2012, 21:48
  5. Drag and drop from list/tree widget to graphics view
    By chaltier in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2012, 04:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.