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. Populate QTreeView from SQL.
Forum Updated to NodeBB v4.3 + New Features

Populate QTreeView from SQL.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.2k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Chrisw01
    wrote on 7 Oct 2017, 23:08 last edited by
    #1

    Hi, I'm trying to populate a QTreeView from SQL database. I have it populating the widget but it is not putting it in a tree.

    Here is my code

    QSqlQueryModel *model = qobject_cast<QSqlQueryModel*>(ui->invoiceTreeViewWidget->model());
        QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
        if(model) {
            model->setQuery("SELECT `Invoice Number`, `Invoice Date` FROM INVOICES ORDER BY `Invoice Number`");
            proxyModel->setSourceModel(model);
            ui->invoiceTreeViewWidget->setModel(proxyModel);
        }
    

    I end up with 7 separate entries instead of them being tree'd together

    Any help would be appreciated.

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Oct 2017, 10:44 last edited by
      #2

      @Chrisw01 said in Populate QTreeView from SQL.:

      QSqlQueryModel

      Hi
      As far as i know this returns as table and not tree and hence you have to use a proxy model to supply the tree indexes
      http://www.qtcentre.org/threads/23698-how-to-display-QSqlQueryModel-in-QTreeView
      or simply build your own model
      http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Chrisw01
        wrote on 9 Oct 2017, 16:30 last edited by
        #3

        Hi, and Thanks.

        I sorta found that out after hours of extensive searching. I went ahead and wrote my own model to populate the tree.

        May not be the correct way but it worked for me..

        Thanks again.

        Y 1 Reply Last reply 25 Mar 2023, 03:46
        1
        • C Chrisw01
          9 Oct 2017, 16:30

          Hi, and Thanks.

          I sorta found that out after hours of extensive searching. I went ahead and wrote my own model to populate the tree.

          May not be the correct way but it worked for me..

          Thanks again.

          Y Offline
          Y Offline
          ytx.cash
          wrote on 25 Mar 2023, 03:46 last edited by
          #4

          @Chrisw01

          Hi there,

          I am a new learner of Qt and I am very interested in learning how to transition from SQL to QTreeView. Would it be possible for you to share your code with me?

          Please let me know if you have any questions or need further clarification.

          C 2 Replies Last reply 25 Mar 2023, 17:25
          0
          • Y ytx.cash
            25 Mar 2023, 03:46

            @Chrisw01

            Hi there,

            I am a new learner of Qt and I am very interested in learning how to transition from SQL to QTreeView. Would it be possible for you to share your code with me?

            Please let me know if you have any questions or need further clarification.

            C Offline
            C Offline
            Chrisw01
            wrote on 25 Mar 2023, 17:25 last edited by Chrisw01
            #5

            @ytx-cash Hello,

            Wow, I haven't messed with that project in years. I will see if I even still have the code when I get home from work. What exactly are you trying to accomplish?

            1 Reply Last reply
            0
            • Y ytx.cash
              25 Mar 2023, 03:46

              @Chrisw01

              Hi there,

              I am a new learner of Qt and I am very interested in learning how to transition from SQL to QTreeView. Would it be possible for you to share your code with me?

              Please let me know if you have any questions or need further clarification.

              C Offline
              C Offline
              Chrisw01
              wrote on 25 Mar 2023, 18:30 last edited by Chrisw01
              #6

              @ytx-cash

              void amsSupport::loadRentalForms() {
                     QSqlQuery sql;
                     QString sqlStatement = "SELECT * FROM RENTALS ORDER BY `Order Number` DESC ";
                     QTreeWidgetItem * child = nullptr; 
                     QStringList header, parentString, childList;
                     QString  previousClass=nullptr;
                     int row = -1;
                     ui->amsSupportTreeViewWidget->clear(); 	// Clear the widget
                     if(sql.exec(sqlStatement)) {
                            header << "Order Number" << "Order Date" << "Customer Name" << "Equipment" << "Serial Number" << "Stock 
                                                 Number" << "Sales Person" << "Store Location" << "Status" << "Remarks";
                           ui->amsSupportTreeViewWidget->setHeaderLabels(header);
                          while(sql.next()) {
                                QString className = sql.value(0).toString();
                                if(previousClass != className)  {
                                           parentString << sql.value("Order Number").toString() << sql.value("Order Date").toString() << 
                                           sql.value("Customer Name").toString();
                                           QTreeWidgetItem *item = new QTreeWidgetItem(parentString);
                                           ui->amsSupportTreeViewWidget->addTopLevelItem(item);
                                          previousClass = className;
                                          parentString.clear();
                                          row++;
                                }
                                childList  << " -> ";
                                childList << sql.value("Order Date").toString();
                                childList << sql.value("Customer Name").toString();
                                childList << sql.value("Equipment").toString();
                               childList << sql.value("Serial Number").toString();
                               childList << sql.value("Stock Number").toString();
                               childList << sql.value("Sales Person").toString();
                               childList << sql.value("Store Location").toString();
                               childList << sql.value("Status").toString();
                               childList << sql.value("Remarks").toString();
                               child = new QTreeWidgetItem(childList);
                               child->setFlags(child->flags() | Qt::ItemIsUserCheckable);
                               ui->amsSupportTreeViewWidget->topLevelItem(row)->addChild(child);
                              childList.clear();
                          }
                     }
                     else {
                          log_error();
                   }
              }
              

              What this does is pull my rental order numbers then adds them to the item based tree view widget one by one until the order number changes then starts a new row.

              Here is the outcome
              tempsnip.png

              I'm sure by now there are better methods of doing this but this is what I did and its been working fine.

              Good luck!

              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