Solved Populate QTreeView from SQL.
-
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
-
@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 -
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.
-
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.
-
@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?
-
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
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!