New to qt/cpp and model/view. Need guidance on custom nested qabstractitemmodel creation with model/view methodology.
-
Hello all,
I'd like to ask for some help with qt's model/view workflow. I come with a C background (and some basic c++/oop knowledge) and I am learning QT/C++ as both an exercise and to build a personal project.
I want to build an interface that consists of a dynamic number of tab widgets, each of which contains a dynamic number of sections(columns), where each of the sections would contain a dynamic number of custom widgets representing custom items. The section display would be something akin to a QListView, but with the list items replaced by custom widgets, and the model would be something like a QStringListModel, where the qlist<string*> would be replaced by something like qlist<CustomItem*>. The end goal is to be able to create/edit/delete new items, sections, groups, rearrange or drag the items from one section to another, and rearrange the sections or drag them to different groups. Below is a graphical representation and a pseudocode of the data classes:
class CustomItem { QString item_text; QString item_description; QPixmap item_icon; } class Section { private: qlist <CustomItem*> internal_item_list; public: add_item(CustomItem*); create_item(); remove_item(CustomItem*) } class Group { qlist <Section*> internal_section_list add_section(Section*) create_section() remove_section(Section*) } class AppData { private: Qlist <Group*> applicationData; public: loadFromJson(QString* filePath); saveToJson(QString* filePath); } main(){ .... AppData *applicationData = new AppData; appData->loadFromJson("~/my_config.json"); setupUi() //draw the graphical representation of all the data .... } void exit(){ //on an exit button click or something similar appData->saveToJson("~/my_config.json"); }
I have started off by creating a custom item widget, a custom section widget, and reimplemented mouse and drag functionality on the section widget to be able to drag an item widget from one section to another (by deleting the widget, sending item data with the drag, and creating a new widget in the destination section), but during my learning i stumbled upon the model/view methodology and now believe that following MVC would be the proper way to go. I have also implemented JSON parsing in each of the classes, so that i can instantiate the applicationData object from a JSON config file.
I have looked at various QT resources, and I am still very much confused about the model/view methodology. So far, i understand i would need to subclass QAbstractItemModel, QAbstractItemDelegate, QAbstractItemView to create models, views and delegates for the group and section data classes. But I am not sure how to tie it all together, and what steps i should take to do everything. I would really appreciate some guidance on what steps i should take to reach my functionality goals and if it is at all possible/reasonable to do what i want with model/view methodology.
So far i have found this https://stackoverflow.com/questions/51439714/qt-nested-model-views
which seems to give an example for a similar problem, and i managed to recreate the project and build the example code. But, im new to C++ and trying to learn oop as i go about creating my gui; meanwhile, the example code given there is extremely difficult to follow as it contains templates, lambdas, maps, vectors, all things that I'm only barely familiar with. The complexity of the example (though i realize it might just be my inexperience) also makes me question whether this is the correct way to approach the problem , so i would appreciate if someone could confirm that the code provided in the link is a good example to follow.Thanks in advance for all the help
-
Hi and welcome to devnet,
It looks like you have some kind of a tree structure for your data. Is that correct ?
It's possible that you are currently doing a bit of over engineering.
From the looks of it you seem to want to have a QTabWidget with one tab for each top level item of your structure. Then the rest seems to be tabular data that could be shown using a QTableView with a QStyledItemDelegate for drawing your bottom data.
Before going further, you have to tell us a bit more about the original data structure that you want to load and store and how you want your widget to look like.
-
@SGaist said in New to qt/cpp and model/view. Need guidance on custom nested qabstractitemmodel creation with model/view methodology.:
Hi and welcome to devnet,
It looks like you have some kind of a tree structure for your data. Is that correct ?
Hello SGaist and thanks for the reply.
Not quite sure on how better to explain my data structure than the pseudocode I presented in the original post. I will attempt at describing all the data i want to have in a bit more detail. As for the structure of the data, i would say it is more hierarchical than a tree (so this https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html#design-and-concepts would not work, as the item types for the children, parents and grandparents would be different).
I would have four classes: allData, group, section, CustomIcon. Going down the hierarchy would look as follows:
-
The allData class would contain a list of groups (in my case i use QList<Group*> groups), and potentially some other variables. The class would have methods to create/destroy the groups, and methods to load/save the current configuration from/to json.
-
Each group would contain a string with its name, a qlist of sections, and it's current position (index) relative to other groups (this is for the ability of saving/restoring the exact positioning of all the items between closing and restarting the application). Again, this class would have methods to create/destroy sections and load/read json.
-
Each section would contain a string with its name, current position, and a qlist of CustomItems. Basic methods again the same.
-
Each CustomItem would contain a string with a title, a string with a description, a QPixmap for an icon, its position and potentially more data in the future. The CustomItem would be the smallest class.
In my view, all of these are completely distinct. There is no inheritance between either of the four classes.
Looking at the "simple tree model example", I believe that would not work, as my problem is different due to each layer being unique. In the example, the TreeItem has a child item of the same TreeItem type - this is not correct for my problem.
Hope this better explains what i'm trying to do.
-
-
Looks like you have several concepts intertwined.
One is purely data that you want to show and then you have your application state. (I know I might appear dense I just want to get you correctly started). -
@SGaist said in New to qt/cpp and model/view. Need guidance on custom nested qabstractitemmodel creation with model/view methodology.:
Looks like you have several concepts intertwined.
One is purely data that you want to show and then you have your application state. (I know I might appear dense I just want to get you correctly started).Sadly, i feel you have not provided any helpful content here to point me in the correct starting direction.
-
Well, your drawing makes me think of the following:
- a QTabWidget for the green part
- each tab will be a QScrollArea containing a QWidget with a QHBoxLayout.
- each blue part will be a QScrollArea with a QWidget containing a QVBoxLayout
- each purple widget looks like either a QListView showing the entries of a custom QStandardItemModel built on top of your CustomItem class
It may also be possible to do everything with custom item views but I would take that on in a second round once you have the purple widget working correctly.