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. How to store data returned by dialog for later use in application?
Forum Updated to NodeBB v4.3 + New Features

How to store data returned by dialog for later use in application?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 739 Views 2 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.
  • S Offline
    S Offline
    Stroopwafel
    wrote on last edited by
    #1

    As you might have guessed I am new to Qt and I'm trying to understand how I should manage data created by a user while the program is still running.
    I feel like this is beyond basic, but I have simply not been able to find anything on this and I feel like I have missed a fundamental aspect of Qt and I have no other idea than to ask here so that someone can explain how to do this basic thing properly.

    Let's say my MainWindow has only two buttons. CreateObject and ManageObjects.

    When the user clicks on New Object they are taken to a Dialog where they can name their object and specify a category:

    When the user presses Ok, the dialog is accepted, when the user presses Cancel the dialog is rejected.
    When the dialog is accepted I run a bit of code which takes the values from the Line Edits using getter functions and stores it in an instance of my own class:

    This is great, but once the interactWithWindowNewObject function goes out of scope, this instance of my Object class is lost.
    I would like to store any newly created instances of my object class somewhere, so that when the user then clicks on Manage Objects, he or she gets a list of all objects created in the past.

    I feel like this is gindergarten basics, but I have not been able to either think of, or find best practices on how to do this.
    I can only think of either creating a global QList<Object> variable, to which I append any newly create instances of the Object class, or constantly reading and writing information from and to files.

    However, reading from and to files for everything defeats the purpose of having a GUI and creating a global QList means that my interactWithWindowNewObject function has to return an instance of the Object class to append to the QList. This seems like bad practice, since the user can also cancel the Dialog, after which nothing should be returned. And a function can not both be of type void and of type Object at the same time.

    What would be the best practices for managing data created/edited/deleted during runtime?

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

      Hi,

      Since you have a MainWindow, I guess that this is your central application widget. So for a simple start your list of object should be stored there. Your first dialog design looks good. You only get the data you want if you clicked the right button. You can then, in your MainWindow create the object and add it to your list.

      Then with your other dialog, just configure it with the adequate values from your object and then do the same as before, take the new values into account only if the dialog is accepted. Update the content of your list accordingly.

      Don't use global variables like that. Most of the time, it's the wrong solution and something to avoid.

      In any case, depending on where you are going, you should take a look at Qt's model/view framework.

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

      S 1 Reply Last reply
      4
      • SGaistS SGaist

        Hi,

        Since you have a MainWindow, I guess that this is your central application widget. So for a simple start your list of object should be stored there. Your first dialog design looks good. You only get the data you want if you clicked the right button. You can then, in your MainWindow create the object and add it to your list.

        Then with your other dialog, just configure it with the adequate values from your object and then do the same as before, take the new values into account only if the dialog is accepted. Update the content of your list accordingly.

        Don't use global variables like that. Most of the time, it's the wrong solution and something to avoid.

        In any case, depending on where you are going, you should take a look at Qt's model/view framework.

        S Offline
        S Offline
        Stroopwafel
        wrote on last edited by
        #3

        @SGaist Yeah that worked perfectly. Your comment regarding Models left me wondering though.

        Would it be proper to use Qt's models to store collections of class objects? I feel like I want to retain the class structure of objects for obvious reason, and at the moment I don't directly see the added benefit of creating a Qt specific model over a list of classes. Or is it just bad practice to store data in QList's in the MainWindow class?

        I'm trying to do things by the book so I won't kick myself down the line when I start doing more complex things.

        1 Reply Last reply
        0
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          Hi,
          that really depends on the data amount, format, what you're going to do with them, etc. For most parts I find built in models quite sufficient. By the rule of thumb if none of the models provided suits your needs you take what's most resembling what is needed and extend that (in my work cases that's when I find QSqlTableModel not quite doing what I want it to do or if the data needs to be delivered in some weird way...).

          For more information please re-read.

          Kind Regards,
          Artur

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

            It's not mandatory just something for you to check.

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

            1 Reply Last reply
            1
            • artwawA artwaw

              Hi,
              that really depends on the data amount, format, what you're going to do with them, etc. For most parts I find built in models quite sufficient. By the rule of thumb if none of the models provided suits your needs you take what's most resembling what is needed and extend that (in my work cases that's when I find QSqlTableModel not quite doing what I want it to do or if the data needs to be delivered in some weird way...).

              S Offline
              S Offline
              Stroopwafel
              wrote on last edited by Stroopwafel
              #6

              @artwaw Yes I just realized that my list of class objects (which would each have their own variables) might just as well be a table with columns and rows. So I suppose a 2D Qt model would be a good way to store this data.

              @SGaist I see, thank you. I will definitely give it a good read since I want to learn best practices now before my apps get complicated.

              artwawA 1 Reply Last reply
              0
              • S Stroopwafel

                @artwaw Yes I just realized that my list of class objects (which would each have their own variables) might just as well be a table with columns and rows. So I suppose a 2D Qt model would be a good way to store this data.

                @SGaist I see, thank you. I will definitely give it a good read since I want to learn best practices now before my apps get complicated.

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                @Stroopwafel My choice of backend for table models is QSqlite - fast, robust. Can be used in memory only, can be used together with filesystem for persistent storage. Depending on your needs it might be worth to explore.

                For more information please re-read.

                Kind Regards,
                Artur

                S 1 Reply Last reply
                0
                • artwawA artwaw

                  @Stroopwafel My choice of backend for table models is QSqlite - fast, robust. Can be used in memory only, can be used together with filesystem for persistent storage. Depending on your needs it might be worth to explore.

                  S Offline
                  S Offline
                  Stroopwafel
                  wrote on last edited by
                  #8

                  @artwaw TBH I'm actually quite familiar with SQLite anyway. I'll give that a shot! Thanks for the help!

                  1 Reply Last reply
                  1

                  • Login

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