TreeView Using QAbstractItemModel - c++

Is there any step to create a model from QAbstractItemModel in c++ and expose it to qml ?
I have seen the examples in QT like SimpleItemEditor etc but there they have given a text as default.I want string names given in c++ using QAbstractItemModel to be shown in treeview.
I donot know how to create a model. PLease help me.

Related

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.

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

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.

How to customize the display of a QListView

I have implemented a list of users in my Qt program, using the model/view principle of Qt. My QListView displays a subclass of QAbstractListModel and so far this works just fine.
Now I would like to customize the display of my user list (display the name on several line, add IP information, and so on: not really relevant, I just want something really custom).
I couldn't find anything in the Qt documentation regarding this: what are my options ?
Note: The items in the list do not need to (can't) be modified, if this can help.
Thank you.
You need to create a new item delegate class to handle the painting. Here is a good answer to a similar question.

Adding an inheritance to an Qt designed object

I have a problem and I want to implement the MVC pattern to my QT application, that's why I need for example to inherite in my QTableWidget about another class like
myClass
{
myMethod();
}
but our QTableWidget is contained by our Mainwidows that is designed by the QT designer and generate an ui_MainWindow class !
Do you know how to do that ?
Is that a method inside the Qt Designer to do that ? or another method ?
Thank you for your answer !
You can use custom objects in QDesigner. Right click on the widget that you want to have as an instance of a something that is not available in designer and select "Promote to ...". In that dialog you can enter a custom class and a include file where the declaration for your class can be found.
BUT Qt implements uses MVC for a lot of the classes look at QTableView as opposed to QTableWidget, QTableView uses QAbstractItemModel as the model class and there is a pretty strict separation between the view and the model. See also An Introduction to Model/View Programming # Nokia