In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "what is the method of realizing multi-component linkage between Qt database and TableView". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. below, please follow Xiaobian's train of thought to slowly deepen, to study and learn "what is the method of realizing multi-component linkage between CAccord Craft + Qt database and TableView".
The Qt database component is linked with the TableView component. In the following case, when the user clicks and selects a row in the TableView component, we query through the name field in that row and associate the query result to the ListView component, while displaying the fields of the selected row in TableView in the LineEdit edit at the bottom of the form. The details of this case are as follows.
First, draw the required controls in the UI interface. Put a TableView component on the left, a ListView component on the right, and three LineEdit components at the bottom. The interface is as follows:
We still need to create two table structures, the table Student is used to store the basic information of the students, and the table StudentTimetable stores the list of courses that each student needs to study, and creates the data table after execution.
Void InitMultipleSQL () {QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (". / lyshark.db"); if (! db.open ()) {std::cout setSelectionModel (theSelection); ui- > tableView- > setSelectionBehavior (QAbstractItemView::SelectRows); / / create data mapping dataMapper= new QDataWidgetMapper (); dataMapper- > setSubmitPolicy (QDataWidgetMapper::AutoSubmit); dataMapper- > setModel (qryModel) DataMapper- > addMapping (ui- > lineEdit_id,0); dataMapper- > addMapping (ui- > lineEdit_name,1); dataMapper- > addMapping (ui- > lineEdit_age,2); dataMapper- > toFirst (); / / binding signal, output / / https://www.cnblogs.com/lyshark connect (theSelection,SIGNAL (currentRowChanged (QModelIndex,QModelIndex)), this,SLOT (on_currentRowChanged (QModelIndex,QModelIndex)) in the bottom edit box when the mouse is selected } MainWindow::~MainWindow () {delete ui;}
At this point, the program runs and gets the data in the table:
Then we need to bind the on_currentRowChanged () event of the TableView table. When the user clicks on a property in the TableView table, the function is automatically triggered. In this function, we complete the filling of other components.
1. Get the row of the current table through the currentIndex method
two。 Query the name in the table through the current line number, and bring it into the StudentTimetable table to check the record in the table.
3. Get the user's data in a loop, extract the timetable field and put it into the QStringList container
4. Associate the data directly into the ListView data table
/ / the processing slot function void MainWindow::on_currentRowChanged (const QModelIndex & current, const QModelIndex & previous) {Q_UNUSED (previous); if (! current.isValid ()) {return;} dataMapper- > setCurrentModelIndex (current) after mouse click; / / get bool first= (current.row () = = 0) at the beginning and end of the record / / whether to first record bool last= (current.row () = = qryModel- > rowCount ()-1); / / whether to record std::cout toFirst (); refreshTableView ();} / / the last record void MainWindow::on_pushButton_2_clicked () {dataMapper- > toLast (); refreshTableView ();} / / the previous record void MainWindow::on_pushButton_3_clicked () {dataMapper- > toPrevious () RefreshTableView ();} / / next record void MainWindow::on_pushButton_4_clicked () {dataMapper- > toNext (); refreshTableView ();}
The final result is as follows:
Thank you for your reading, the above is the content of "what is the method of realizing multi-component linkage between Qt database and TableView". After the study of this article, I believe you have a more profound understanding of what is the method of realizing multi-component linkage between Qt database and TableView, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.