How to manage multiple SQLite databases in a Qt C++ program? - c++

I am writing a program in Qt creator. I have a treeview and a tableview. Each time an item in the treeview is clicked, I need to update the tableview. The problem is that the tables realted to each tree item is different. Since the tableview works with SQLite databases, I have decided to create a database and a qsqltablemodel for each of the tree items and set the qsqltablemodel for the tableview upon each click on the tree item. I guess the tableview should update then. To implement this, I need at least 60 databases and models and am thinking if there is any issue with having mutiple databases and how to manage them effectively.

Related

Access sqlite database using QtableWidget c++ visual studio

I am new to stack overflow, I am using c++, I need to connect with my sqlite database which i am successfully accessing, Now I want to display that information (contents of tables from sqlite) into a table either Qtablewidget or Qtable view. I am novice to Qt. How should I do this?
How can I display information in QTablewidget in c++?

QListWidget sharing the same model

If I understood the concept of the Qt models correct, then I can have multiple views, sharing the same model, so that when data is updated in the model, all views using it, will also update their view accordingly right?
Now I have multiple widgets in my application, which should have individual selections, but they should operate on the same underlying data. So when a row is added in one of the panels, the others should be able to display this new row as well.
Since the QListWidget provides all the features that I need, there would be no point in writing my own model and use it with a QListView. But I realized that QListWidget doesn't allow me to change the model, because the setModel()method is made private.
So is there some way that I can achieve this, without the need of writing a full model on my own? I'm rather new to Qt so maybe there is a ready made general purpose model, that I can use? But so far I haven't found one.
You are trying to use QListWidget to set your own model which is not possible.
You are better off using the MVC pattern the QT imposes. You can refer here on how to use the MVC pattern. This way would be more manageable and correct. Also you can make of the QStandardItemModel or the more general QAbstractListModel or QStringListModel model for lists. You can refer here for more details
But to answer your question yes they is a hack you can use.
Create a QListWidget and treat that widget as your model.
For other views create a QListView and set the model that is returned by the QListWidget
.
For e.g. refer
QAbstractItemModel* model = listWidget->model();
listView->setModel(model);
listView_2->setModel(model);
Then you can use the listWidget as your model. Any operation (add/delete) performed on the listWidget will also affect the listView and listView_2.
I am still of the opinion that instead of the hack creating your own model would be better and more correct.

Populating a QTreeView on an existing application with a QFilesystemModel

I have an existing Qt application with buttons and other widgets on it. I would like to add a QTreeView (or QTreeWidget) to the application, and populate it with a QFilesystemModel.
I have seen examples and read the Qt documentation on using QTreeView and QTreeWidgets but they are always just creating a standalone QTreeView on its own, which I can replicate fine.
I used QtDesigner to add a QTreeView on my application, but i cannot work out how to use the QFilesystemModel to populate it.
How do I access the QTreeView on my application and set its model to the QFilesystemModel?

Add QSqlQueryModel or index value to QListWidget

Is there a way to add my QSqlQueryModel to a QListWidget? If not is there a way to add the database row id to a QListWidgetItem?
I cant figure this out from the documentation on QListWidget. I can add items to the list but can only think to use the row number of the item as the hidden index which wont work if index numbers are out of order.
OK, here is the deal:
The QListWidget is part of the older Qt3 data widget set.
The widget you actually want is part of the newer "interview" Qt MVC framework. It is called QListView.
It can be a bit confusing.
In general: A "QxxxxView" widget can be bound to a "QxxxxModel" object, such as the QSqlQueryModel you asked about.
See:
http://qt-project.org/doc/qt-4.8/qlistview.html
And:
http://qt-project.org/doc/qt-4.8/model-view-programming.html
Lot's of good examples. For instance:
http://qt-project.org/doc/qt-4.8/sql-querymodel.html
Good luck.
:)

how to create ms-access like continuous subforms (widgets) in Qt?

I'm thinking of porting my access application to Qt. I am interested to learn how to do continouos subforms, sub custom widgets for presenting/editing/inserting data from recordset in a verically scrollable non datagrid fashion. Meaning I could put button, label, combo, lineEdit... whatever, for every record.
I like QTableView and delegates. I just don't know if it could be modified to fully emulate access subform.
Sidequestion(maybe the same answer)... how do they DO those continuous forms in access under the hood.
thanks
... not real application data in that example recordset
Qt MVC is probably the best/easiest answer for your question ( http://qt-project.org/doc/qt-4.8/model-view-programming.html ), and with QTableView you should be able to achieve what you want.
Another solution could be: if you have a fix set of column items in every row you could simply design a QWidget with the contents of the row and paste your items (rows) into a QVerticalLayout.
Although I would sugget to try with MVC because that's the preferred way and in this case you could even port it to use QML UI if you want (while you can use the same data classes for the 'backend'). QML is definitely the best approach for (even slightly) animated UIs, and it's mature enough to use it already (it's part of Qt 4.8 and will be the 'star' of Qt 5).