In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use SqlRelationalTable correlation tables in Qt database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, we create two tables, one Student table stores the student's name and course number, and the other Departments stores the department name corresponding to each number, and runs the code to complete the creation.
Void MainWindow::InitSQL () {QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (". / lyshark.db"); if (! db.open ()) return; / / execute SQL to create table db.exec ("DROP TABLE Student") Db.exec ("CREATE TABLE Student (" id INTEGER PRIMARY KEY AUTOINCREMENT, "" name VARCHAR (40) NOT NULL, "" departID INTEGER NOT NULL "); / / insert data db.exec one by one (" INSERT INTO Student (name,departID) VALUES ('zhangsan',10) ") Db.exec ("INSERT INTO Student (name,departID) VALUES ('lisi',20)"); db.exec ("INSERT INTO Student (name,departID) VALUES (' wangwu',30)"); db.exec ("INSERT INTO Student (name,departID) VALUES ('wangmazi',40)"); db.exec ("DROP TABLE Departments") Db.exec ("CREATE TABLE Departments (" departID INTEGER NOT NULL, "department VARCHAR (40) NOT NULL"); db.exec ("INSERT INTO Departments (departID,department) VALUES"); db.exec ("INSERT INTO Departments (departID,department) VALUES") Db.exec ("INSERT INTO Departments (departID,department) VALUES"); db.commit (); db.close ();}
After initialization, you will get two data tables, which are associated by departID, as follows:
After the creation is completed, we can directly implement the binding in the program constructor. The code is very simple as follows:
/ / https://www.cnblogs.com/lysharkMainWindow::MainWindow(QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); InitSQL (); / / Open database QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (". / lyshark.db"); if (! db.open () return; this- > setCentralWidget (ui- > tableView) Ui- > tableView- > setSelectionBehavior (QAbstractItemView::SelectItems); ui- > tableView- > setSelectionMode (QAbstractItemView::SingleSelection); ui- > tableView- > setAlternatingRowColors (true); / / Open Datasheet tabModel=new QSqlRelationalTableModel (this,DB); tabModel- > setTable ("Student"); / / set Datasheet tabModel- > setEditStrategy (QSqlTableModel::OnManualSubmit); / / OnManualSubmit tabModel- > setSort TabModel- > setHeaderData; / / query relational data table for code fields / open Departments table, associated with ID and department tabModel- > setRelation ("Departments", "departID", "department"); theSelection=new QItemSelectionModel (tabModel) Ui- > tableView- > setModel (tabModel); ui- > tableView- > setSelectionModel (theSelection); ui- > tableView- > setItemDelegate (new QSqlRelationalDelegate (ui- > tableView)); / / set the default proxy component tabModel- > select () for relational fields; / / Open datasheet} MainWindow::~MainWindow () {delete ui;}
The final binding effect is as shown below:
This is the end of the introduction of "how to use SqlRelationalTable correlation tables in Qt database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.