QTableView - samples - c++

How to use QTableView in Nokia Qt SDK (for mobiles). I referred some of documents but still I am not clearing about the QTableView. Please any one suggest how to use the QTableView.
I want to show the QTableView with three columns.

For the table data, you need to implement a model which will hold the data. If you don't require anything special, you can just subclass QAbstractTableModel.
Quoting the most important parts from the documentation:
When subclassing QAbstractTableModel,
you must implement rowCount(),
columnCount(), and data().
Editable models need to implement
setData(), and implement flags() to
return a value containing
Qt::ItemIsEditable.
You haven't specified where you get the data you are going to show in your table. That determines how you need to implement the required functions.
For even more simple model, use QStandardItemModel which already has a basic implementation for all required functions.

Related

Edit QTableView with combo box in column

I created my own TableView and implemented model for it, etc. Finally, I started to edit values : it went well with flags and setData functions reimplemented, but I can only edit string/int values. I need to add possibility to select from combo box there. I saw topics like this one, but QTableView doesn't have a setCellWidget method.
Is there any possibility to implement it with QTableView or do I have to switch to 'TableWidget' and re-do my work (which would be problematic)?
QTableView has a method setIndexWidget() to display a widget in the particular cell. But in your case you should use a delegate derived from the QItemDelegate and reimplement the createEditor() method to create your combo-box.
Adding to Tomas's answer you will also need to reimplement the setEditorData and setModelData functions.
setEditorData is used to get the data from the model and set the initial value of the editor.
setModelData updates the data in the model according to the data entered in the editor.

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 display a selected date and a number in Qt

I'm currently working with the QCalendarWidget and I need some ideas to accomplish the following.
What would be the best way to add the selecteDate from a QCalendarWidget and a number to some sort of table. What I want is basically to have a list of dates with a number attached to each date, these numbers will be added together and the result will be displayed in a QLabel, I also want to be able to delete rows and again update the QLabel every time a row is deleted.
I also want to be able to save the list to an external file.
Should I use a QStringListModel or a QTableView?
How would you accomplish this?
I'm not expecting any code just a general procedure.
Please see the attached image for more details.
Should I use a QStringListModel or a QTableView?
You may want to familiarize yourself with the model/view framework. To put it simply, a model is the actual data that you have and it is independent of how it should be displayed. A view is a particular display implementation of a model. So you could use a model like the QStandarItemModel to store your String+number data and display the model in a QTableView.
Model/View Tutorial from Qt website here
QStandardItemModel class here. Has a simple example inside there.
And, for writing and reading the data to a file, I suggest you could use the QXmlStreamWriter/Reader classes. Refer to Qt xmlWriter/xmlReader

Using Qt Model/View with non-table like data and non-table/list UI?

I've been reading about Qt's Model/View framework. I find it really helpful on working with tabled data like tables from a database. My question is: will it be useful for non-table data like property list or just some bunch of data of various types? If so, how should I approach it?
The goal is to come up with an editor for some property list like data. The list is constructed at runtime and the elements are of various types (numbers, strings, booleans, and file paths, to name a few). Each element is basically a name-value pair. The name, type, and restrains (limits for example) for each element are defined at compile time. They will be assembled at runtime into different lists depending on user input. And the list of elements can change during the edit session.
The UI will most likely be combination of various pre-designed widgets assembled according to user input. They may not be list or table views.
Some pointer to design pattern or examples are also much appreciated. Thanks.
I don't see a problem with MVC framework in QT for doing that.
Basically the difference between a standard table display and this is that you create a list dynamically akin to a map of:
QMap<QString, QVariant> property_map;
You can do a:
QList<std::pair<QString, QVariant>> property_list;
which you can then use to display in a table the property. The best way would probably be:
struct {
QString prop_name;
int prop_type;
QVariant prop_value;
};
QVariant basically will provide you with a single abstraction class for data storage and it is actually what's being returned by the data() function inside the QAbstractItemModel which you might be reimplementing.
So basically you will take a property list and boil it down to the same table like data as the database.
AMENDED
If you have a Widget that you want to have this widget populated with other predefined widgets you are quite likely to have multiple problems unless widgets are of same or well defined size.
What you can do is in you Display widget define a layout like: QGridLayout or other possible layouts and then add your other widgets to it using some set of parameters, which could be done but can be somewhat of a pain.
The other approach that you may take is to place all property widgets up front on the display UI and simply turn the ones you need on and the rest off, but this only applicable if you have a well defined limited number of pre-designed widgets.
I've been using Model/View framework for quite some time now and I usually implement my own models with a backend based on Qt containers (vectors, list, etc). Even if data eventually comes from a database, working with (e.g.) a vector of database ids can dramatically improve performance (and sometimes is the only way you can do).
This trivial example from Qt docs (see "Creating a Custom Model) is the point where I started and shows how to use a QStringList as a backend for a custom model.
Once defined your model you can define your custom Views, which will draw arranged widgets based on the content of the model underneath.
When the model change, your view will change accordingly rearranging widgets when necessary.
Leveraging QVariant capabilities you should be able to render the proper widget for every datatype (e.g. a QSpinBox for a float a QComboBox for a QStringList, and so on...)

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.