Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help a newbie to deal with QTreeView & Model

Help a newbie to deal with QTreeView & Model

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeviewqsqltablemodel
2 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Sasha.SS Offline
    Sasha.SS Offline
    Sasha.S
    wrote on last edited by Sasha.S
    #1

    Hello!
    Help build the tree from sql.
    There is a table:
    | id | id_parent | name |
    | 1 | | footwear |
    | 2 | 1 | sandals |
    | 3 | 2 | red |
    | 4 | 2 | blue |
    | 5 | 1 | sneakers |
    | bla | bla | bla |

    From this table, construct a tree:

    ++ footwear
    ++++ sandals
    ++++++ red
    ++++++ blue
    ++++ sneakers
    ++++ bla, bla, bla

    I already have the code that displays the contents of the table.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include "QMessageBox"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("../../task/task.sqlite3");
        if(!db.open())
            QMessageBox::critical(this, "Wirning DB", "No database connection!");
    
        model = new QSqlTableModel(this);
        model->setTable("hierarhy");
        model->select();
    
        qDebug() << model->lastError().text();
        ui->treeView->setModel(model);
    }
    
    MainWindow::~MainWindow()
    {
        db.close();
        delete ui;
    }
    

    What should be changed in the code to build the tree.
    Sorry for my English. I use a translator. Please provide sample code.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elveatles
      wrote on last edited by
      #2

      I don't know if there are any Qt models that support SQL for trees. I think they were all built for tables so you might have to build one yourself.
      Have you checked out the Simple Tree Model example? I found it pretty helpful: http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html . Also, if you find it easier, you can use a QStandardItemModel instead of a QAbstractItemModel.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved