QTableView Empty ?
-
Hi
Is the database connected?
Anyway, the topic is treated here.
https://forum.qt.io/topic/86139/insert-record-in-qsqltablemodel-with-qtableview -
@R-P-H said in QTableView Empty ?:
What am i doing wrong ?
QVector<QSqlRecord> records;
... records[0] - but the vector is empty.In debug mode you will likely hit an assertion here. In release it's undefined behavior.
-
@mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.
I just want to easily display them in a table without manually looping through fields...
Do I need to initialise a dummy database or something ?
-
@Christian-Ehrlicher said in QTableView Empty ?:
@R-P-H said in QTableView Empty ?:
What am i doing wrong ?
QVector<QSqlRecord> records;
... records[0] - but the vector is empty.In debug mode you will likely hit an assertion here. In release it's undefined behavior.
The QVector of records is populated with data, it is not empty. I have double checked that.
-
@R-P-H said in QTableView Empty ?:
I have double checked that.
Your code example shows something different, therefore my comment. insertRecord() returns a bool which you should check.
-
@Christian-Ehrlicher said in QTableView Empty ?:
@R-P-H said in QTableView Empty ?:
I have double checked that.
Your code example shows something different, therefore my comment. insertRecord() returns a bool which you should check.
Yes I didn't show how records gets populated but there is data inside. I did check the bool and it seems to be returning false. I can't understand why though...
-
@R-P-H said in QTableView Empty ?:
@mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.
I just want to easily display them in a table without manually looping through fields...
Do I need to initialise a dummy database or something ?
Hi,
If you don't have any database then why are you using a SQL model ?
Before going further, do you want to store your data in a database ? Or just show them in a tabular fashion ?
-
@SGaist Mainly I just want to display it in a tabular fashion but without the work involved in looping through each QSqlRecord and corresponding field.
Considering the data is stored in QSqlRecords I thought using QTableView would be the easiest way to achieve that...
-
You still need a valid database to insert your records in.
You can use the SQLite plugin for that and the in memory database if you don't want to store in on disk.
Note that you still have to create the table before putting something in it.
-
Before going further, one thing is not clear... Where do you get these records from ?
-
Then why not use a QSqlTableModel ?
Or a QSqlQueryModel ?There would be no need for that vector of records.
-
So you can either do as suggested and create that minimal database with the table needed to show your data or use a QStandardItemModel where you put the content of all these records.
-
@SGaist For QStandardModel I don't see a way to directly make use of my QtSqlRecords, does that mean I have to loop manually through the fields ? Would it still be shown in QTableView ?
For making a minimal database do I need to set a database name?
I will try to implement the other model on the actual dB as well....
-
Yes you do, or you can make your own custom QAbstractTableModel that will return the content of your vector of records.
Yes you do. However you can use an in-memory database if you don't want to have another database in a file.