In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "what is the method of binding the Qt database and TreeView components". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of binding a Qt database to a TreeView component?"
We first take the TreeView component as an example to briefly introduce how to bind the component to data. First, we need to create a table and insert several test records, and run the following code to build the database and build the table.
# include # include void Init () {QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (". / lyshark.db"); if (! db.open ()) {std::cout treeView- > setModel (qryModel); and ui- > treeView- > setSelectionModel (theSelection); display the model in the TreeView component as follows:
# include "mainwindow.h" # include "ui_mainwindow.h" # include # include / / define data model pointer QSqlQueryModel * qryModel; / / data model QItemSelectionModel * theSelection; / / Select model QDataWidgetMapper * dataMapper; / / data interface mapping MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this) QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (". / lyshark.db"); if (! db.open ()) {std::cout lastError (). IsValid () {return;} / / set the TableView header data qryModel- > setHeaderData (0Q QT qryModel- horizontal, "ID"); qryModel- > setHeaderData (1Q QT QSQLITE horizontal, "Name") QryModel- > setHeaderData; / / bind data to the model theSelection=new QItemSelectionModel (qryModel); ui- > treeView- > setModel (qryModel); ui- > treeView- > setSelectionModel (theSelection); ui- > treeView- > setSelectionBehavior (QAbstractItemView::SelectRows);} MainWindow::~MainWindow () {delete ui;}
After running the code, the program fetches the result from the database and outputs it to the TreeView component:
At this point, I believe that you have a deeper understanding of "what is the method of binding C _ Qt database to TreeView components". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.