Solved update QSqlDatabase and QTableView
-
@Mogli123
If the data only comes from an array, do you need to save it in a database at all? You can tie aQTableView
to a memory array without involving SQL/a database at all.Not sure what your best edit strategy should be, given that you won't allow user to make any changes from the UI view anyway....
-
@JonB
Thank you for your help.No, it isn't necessary to save the data in a database.
Yes, I have to save the data in a database.
I tried it this way because that is the only way I know...@JonB :
"You can tie a QTableView to a memory array without involving SQL/a database at all."
Is that a "better and easier" way to manage my problem?
How can I do this?
Furthermore, with the database I had the possibility to change the header data.
In addition, at this time I have some hidden column with have for example the color information in it, which I need.
This color information isn't in the array.
Is that with your way possible, too?the table should look something like that
Sorry if this is basic.
-
@Mogli123
Let's take a step back!I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?
Forget Qt etc. for the moment. Do you or do you not wish to preserve that data in a database for some future purpose? If the answer turns out to be "No, I simply want to display the data in a table in the UI, when the program exits I'm happy for it to vanish" then you do not want to use a database/SQL! Instead look at http://doc.qt.io/qt-5/qstandarditemmodel.html, which is the basis for models which do not need SQL storage.
-
Tank you.
*I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?
Yes, exactly.
I don't need the data in the future.
I only have to display the data like in the picture, but not all of them. Some data I only need for the decision which color the row need, or other decisions. This data are at this time in hidden columns. -
@Mogli123
So like I said: get rid of all SQL stuff, start out fromQStandardItemModel
(can be sub-classed if required), use that as yourQTableView
's model. (I said earlier you could use an array/QVector
/whatever directly; I think you actually want to copy your data into aQStandardItemModel
.) You can then insert/delete/update rows as your data arrives, and the view will update correspondingly. -
@JonB
Thank you very much for your help.I create my own model like shown in my code above.
Is this with the QStandartItemModel possible on the same way?
I need that for manipulating the data.How can I insert the data from my array (there are only qint32 in it) and how are they updating?
Whit signal and slots?I know that are many questions and I'm very thankful for your help. -
My boss just told me that he wants the data in a database.
He knows that it is not necessary at the moment, but later he needs the functions of a database.
So back to my original question, how can I update the database and the tableview?I'm so sorry for this wrong information.
-
@Mogli123
So then I said earlier:You should update the model (QSqlTableModel, your TableCustomise) tied to the view with whatever data (e.g. http://doc.qt.io/qt-5/qsqltablemodel.html#setData, http://doc.qt.io/qt-5/qsqltablemodel.html#insertRecord) then the view will redraw to reflect that.
You need to read those, look at examples etc.
-
Can you give me a smal example pleas how I have to do that?
That will be very helpfull.Thank you very much.
-
@JonB
Thank you for your help:)
I tried it like this and it seems that it workscpp ... myTableCustomiseToolChange->setEditStrategy(QSqlTableModel::OnFieldChange); ... //Methoden ... void test_screen::functionFuerTestwert() { arrayCustomiseToolChange[1] ++; myTableCustomiseToolChange->setData(myTableCustomiseToolChange->index(1,2), arrayCustomiseToolChange[1]); }
is that the right way?
-
@Mogli123
Yes, you seem to be using the model'ssetData()
to alter values, which should be automatically reflected in the view, as per your original question. -
@JonB
Thank you very much:)Have a nice day.