Nominate our 2022 Qt Champions!

Proper way to create Desktop Apps with modern Qt?

  • Hello,

    I want to create a simple App, basically it stores people (Name, Birthday, Wishes, etc.) and allows you to view/add/edit/delete, also it would remind you of upcoming birthdays but that is trivial once you have the right architecture.

    It could just as well be a shopping list app or a basic notes app, it doesn't matter. You have some data and want a view/add/edit/delete interface for it.

    Right now I have a class Person, which represents the type of data I want to store. I store the people in a QMap<QString, Person> (member variable of MainWindow, ugh) where the key is the name of the person. I have several locations inside my app where I would like to have a List Widget/View displaying all names by which I can select/filter the various records. Picture a list on the left where I can select various names and on the right the respective data would be displayed. I believe in Qt you would use a list view, but how can I use the keys inside my QMap<QString, Person> as a Model? I would maybe have to use a separate QStringListModel and update it accordingly? Should I subclass QAbstractItemModel and create a custom Model for these records? I guess I'm not really sure how professionals create Qt apps that are not trivial demo apps with 2 LineEdits and an "Add" button, where I can just use "Go To Slot" from inside QtDesigner.

    Thanks in advance!

  • There are 2 macro frameworks:

    • QAbstractItemModel is the data
    • QAbstractItemModel is an interface to the data

    In the first one you would create a model (using QStandardItemModel, for example) to store the data directly. In your case insert 1 column, insert as many rows as you have Persons, use setData to store QString in the DisplayRole and Person in the UserRole. In this case you don't need the QMap, the model already handles your data.

    The second requires you to keep your data in the QMap, subclass QAbstractItemModel and manually manage events that affect the map. While on paper sounds easy enough, it becomes unmanageable fast when you think that every time you call QMap::erase you also have to remember to call beginRemoveRows/endRemoveRows.

    For this reason I personally always use the first way.
    I'd be more than happy to hear other opinions on the matter though

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To add to @VRonin, from what you described, it looks like you should consider using a database. Likely SQLite which is available on all platforms.

    In any case, how are you planning to save your user data ? From your description, as soon as the application is exited, you loose everything you have done.

  • @SGaist Aaaah yes I forgot, I just use QDataStream and overload the operator<< for the Person class. I wanted to avoid Databases, because I would have to model it and my experience is limited. The only database experience I have is MySQL and PHP. For me the main reason to not use a database atm is my limited number of entries (likely less than 100, surely less than 1000).

  • In this case, if you decide to use the first approach, you can use BinaryModelSerialiser to save/load your model

  • @VRonin So I tried the first approach and it works well so far. I want to be able to edit records belonging to each person as well, I will try using a QStringListModel as member of the Person class for that. I understood the 3 different categories described in the Qt YouTube video with (Data/Model/View), (Data/Model->View) and (Data->Model->View) now, too.

Log in to reply