Qt: How to link form data with Table - c++

I'm new to Qt and would like to know how to link data from a form I created to a table. I also created a QTableWidget. I understand I would need to implement some SQL code, but I am wondering if there is a simple method to map the data. Thank you for your help in advance.

Using the QTableVIew on top of a QAbstractTableModel is a good preferable solution if you want your application to be built with the MVC architecture. You need to create a new class which inherits the QAbstractTableModel. If so, there are some virtual functions such as setData(), data(), rowCount(), columnCount() and headerData() which you need to implement to populate the table with the database table.
I suggest you look into the following classes
QSqlDatabase
QSqlQuery
QAbstractTableModel
After implementing the class which inherits QAbstractTableMode, you could setup a TableView widget and set its model to display the contents.
QTreeView *view = new QTreeView(this);
view->setModel(tableModel);

Related

Cached QSqlQueryModel to mimic OnManualSubmit

I would like to implement a feature like the EditStrategy available on the QSqlTableModel but on a QSqlQueryModel because I have complex queries.
I have already created my subclass of QSqlQueryModel implementing the setData() following the documentation and various examples. However I would like have a feature like the OnManualSubmit strategy available on the QSqlTableModel class.
Any idea how to implement this caching? I also need to have the cached changes available in other view.

Qt: model/view concept with text browser

I am writing an application in Qt with C++. In this application I have to display an amount of data in a text box.
Is there a way to use QTextEdit or QPlainTextEdit with the model/view concept in Qt? I only found list, tree, or table View classes with mvc functionality.
Is there a way to use QTextEdit or QPlainTextEdit with the model/view concept in Qt?
No.
For using model/view concept you need use already existed classes which inherit QAbstractItemView (such as: QColumnView, QHeaderView, QListView, QTableView and QTreeView) or inherit your custom class.
Have a look at the Qt documentation. There are the options you have:
http://doc.qt.io/qt-4.8/model-view-programming.html
Models
QAbstractItemModel provides an interface to data that is flexible
enough to handle views that represent data in the form of tables,
lists, and trees. However, when implementing new models for list and
table-like data structures, the QAbstractListModel and
QAbstractTableModel classes are better starting points because they
provide appropriate default implementations of common functions.
Views
QListView displays a list of items, QTableView displays data from
a model in a table, and QTreeView shows model items of data in a
hierarchical list. Each of these classes is based on the
QAbstractItemView abstract base class.
Controller
QAbstractItemDelegate is the abstract base class for delegates in
the model/view framework.

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.

How to add string/numerical sorting to QSortFilterProxyModel derived class

I have a class derived from QSortFilterProxyModel, however when I click on the tabs of the table view for sorting nothing happens. Any suggestions on how I can add sorting feature to my custom class that inherits from QSortFilterProxyModel?
It is well explained in the Qt documentation. This link points to the Qt 4.8 online reference.
The sorting feature is achieved either by implementing sort() in your model, or by using a QSortFilterProxyModel to wrap the model.
According to the question you're using the second approach. The class QSortFilterProxyModel provides a generic sort() reimplementation and also allows to achieve custom sorting behavior by subclassing QSortFilterProxyModel and reimplementing the lessThan() method.
My suggestions:
1- Make sure the proxy model is acting like a proxy. (it is sitting in the middle of the view and the real model).
QTreeView *treeView = new QTreeView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
treeView->setModel(proxyModel);
2- Make sure to enable sorting in the view (the default value is false).
treeView->setSortingEnabled(true);
3- If you needed to reimplement a member, make sure you have done it correctly.
I hope this helps.

How to use Model for QCombobox

I want to use QCombobox as a the Combobox of Swing in Java. So i need to use Model for holding my object. How can i hold my object in QCombobox. (I think that I should hold data in Model because QCombobox was designed according to MVC Pattern ... )
Any help will be appreciated.
Depending on what you want to display with your QComboBox, you'll need to write your own model, inheriting QAbstractListModel, reimplementing rowCount()and data().
Then, use QComboBox::setModel() to make the QComboBox display it.
If you just want to display strings, you can use a QStringListModel, provided with Qt.
You can add a model to your QCombobox by using the setModel function. You can use a predefined model or create your own by inheriting from QAbstractItemModel.
Your model will contain your object to separate display from data.
Qt uses a simplified version of MVC which only has the Model / View parts.
You can use one of the provided subclasses of QAbstractItemModel if you don't need any specialized behaviour, which one to use depends on whether you keep your data in a file system or a data structure in memory.
You should read up the whole section on model/view programming in the Qt documentation.