Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Proper way to create Desktop Apps with modern Qt?
Forum Updated to NodeBB v4.3 + New Features

Proper way to create Desktop Apps with modern Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UpsJustInhaledQt
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      U 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        U 1 Reply Last reply
        2
        • SGaistS SGaist

          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.

          U Offline
          U Offline
          UpsJustInhaledQt
          wrote on last edited by UpsJustInhaledQt
          #4

          @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).

          VRoninV 1 Reply Last reply
          0
          • U UpsJustInhaledQt

            @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).

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3
            • VRoninV VRonin

              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

              U Offline
              U Offline
              UpsJustInhaledQt
              wrote on last edited by UpsJustInhaledQt
              #6

              @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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved