Solved How to connect QTableView to local file
-
XML format?
-
@tovax The format is not really important, that's up to you to define which one is more appropriate for the type of your data. That being said, IMHO ini file are more appropriate for storing settings than data. So you can use CSV, XML, JSON, SQL or any of this kind of format, or even your custom format using plaintext. Note you can also use binary files alternatively.
I saw in your code that you are opening the ini file each time
data()
is called ni your model. This is probably a bad idea because it will constantly accessing the disk. Instead you could read data from the file only once and store it's content in memory, like in a QList or QVector, a custom class or anything else. Then you can return data from there in modeldata()
methodCan you explain a little bit what you are trying to achieve ?
-
@Gojir4 Thank you very much for reply, I have achieved store contents in QList, but the data must be saved realtime, so I guess the data should be stored in file directly.
-
@Gojir4 INI format is very difficult to insert or remove some rows.
-
@Gojir4 The file is used for CNC(Computer numerical control). In order to prevent sudden power failure, the data needs to be saved to the hard disk in real time.
-
- save data in real time;
- easy to insert or remove rows (column count is constant)
- easy to modify cell data
-
@tovax said in How to connect QTableView to local file:
@Gojir4 The file is used for CNC(Computer numerical control). In order to prevent sudden power failure, the data needs to be saved to the hard disk in real time.
I'm not sure that's correct.
-
@tovax OK, maybe a database would by a better solution in this case. But anyway you can save you content in file when you data have changed from the model, but you don't need to read them.
The easiest way to insert content in a file is to replace completely with new content. but it depends also of the size of your file...
-
@Gojir4 Thanks, It's very helpful to me. I'll reconsider the implementation.
-
@tovax
Just a couple of points, re-iterating the sort of thing @Gojir4 is saying.A
.ini
file is not an ideal candidate for treating as made of rows & columns, because it really isn't. You would, for example, have to implement your own "mapping" from row numbers to the keys (and vice versa), because they don't have any ordering. If you did implement that,insert
/removeRows()
would then be implementable. And it won't work at all if you want, say, the[Sections]
which.ini
file allows for.If ever you put a model on top of an external "text file", then unlike for an actual row-based database if you want to make any kind of change you must rewrite the whole of the file when saving, you cannot actually do any insert/update/deletes on actual lines in the file.
If you wish to manage settings for your own app --- as opposed to edit existing, non-Qt
.ini
files --- you would be best doing that throughQSettings
, not dealing with the text file yourself. -
@JonB Thank you very much for your reply, I got it. Based on your and @Gojir4 's suggestions, I have decided to use the database to solve this problem.
I have done the mapping between QSqlTableModel and QTableView, but I have several databases. I don't know how to update tableview after changing the database. The simple code is as follows:/* 1: Database file */ D:/xyz-1.db, D:/xyz-2.db, D:/xyz-3.db /* 2: Defines */ QSqlDatabase database; QSqlTableModel *tableModel = nullptr; QTableView *tableView = nullptr; /* 3: Database and TableModel */ database = QSqlDatabase::addDatabase("QSQLITE", "connectionTemp"); database.setHostName("www.qt.io"); database.setDatabaseName("D:/xyz-1.db"); // xyz-1.db database.setUserName("qter"); database.setPassword("123456"); if (database.open()) { tableModel = new QSqlTableModel(this, database); tableModel->setTable("MyTable"); tableModel->setEditStrategy(QSqlTableModel::OnFieldChange); tableModel->select(); } else { qDebug() << __FUNCTION__ << QString("Error!"); return; } /* 4: TableView */ tableView = new QTableView(this); tableView->setModel(tableModel); /* 5: Change database on pushbutton clicked */ void onPushbuttonClicked(void) { database.setDatabaseName("D:/xyz-2.db"); // xyz-2.db // How to update TableModel and TableView please? }
-
@tovax said in How to connect QTableView to local file:
How to update TableModel and TableView:
Step1: close database
database.close();
Step2: set database name
database.setDatabaseName(dbName); if (database.open() == false) { }
Step3: clear tableView's model
tableView->setModel(nullptr);
Step4: recreate tableModel
tableModel = new QSqlTableModel(this, database); tableModel->setTable("MyTable"); tableModel->setEditStrategy(QSqlTableModel::OnFieldChange); tableModel->select();
Step5: set tableView's model
tableView->setModel(tableModel);
-