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. update QSqlDatabase and QTableView
Forum Updated to NodeBB v4.3 + New Features

update QSqlDatabase and QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 3.0k Views
  • 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.
  • M Mogli123

    @JonB

    Thank you for the quick answer.
    Sorry my English is not the best.
    I hope I can write it understandable.

    @JonB : I have no idea what "It would be enough if the data were all 5-10 seconds updating" might mean.

    I only would say that it isn't necessary to update the data very quickly.
    It is enough to update the table for example all 5 seconds.

    The data for the database only comes from the variable array.
    It isn't possible for the user to change any data with the tableview.

    the edit strategy should look like this.
    Is that right?

    myTableCustomiseToolChange->setEditStrategy(QSqlTableModel::OnFieldChange);
    

    Have you an example code?
    I'm new, therefore it is very diffucult for me to manage my problem with your links.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @Mogli123 said in update QSqlDatabase and QTableView:

    The data for the database only comes from the variable array.

    How often is this array updated?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mogli123
      wrote on last edited by
      #5

      That isn't defined at this moment, but not faster than every 5 seconds.

      JonBJ 1 Reply Last reply
      0
      • M Mogli123

        That isn't defined at this moment, but not faster than every 5 seconds.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #6

        @Mogli123
        If the data only comes from an array, do you need to save it in a database at all? You can tie a QTableView to a memory array without involving SQL/a database at all.

        Not sure what your best edit strategy should be, given that you won't allow user to make any changes from the UI view anyway....

        M 1 Reply Last reply
        0
        • JonBJ JonB

          @Mogli123
          If the data only comes from an array, do you need to save it in a database at all? You can tie a QTableView to a memory array without involving SQL/a database at all.

          Not sure what your best edit strategy should be, given that you won't allow user to make any changes from the UI view anyway....

          M Offline
          M Offline
          Mogli123
          wrote on last edited by Mogli123
          #7

          @JonB
          Thank you for your help.

          No, it isn't necessary to save the data in a database.
          Yes, I have to save the data in a database.
          I tried it this way because that is the only way I know...

          @JonB :
          "You can tie a QTableView to a memory array without involving SQL/a database at all."
          Is that a "better and easier" way to manage my problem?
          How can I do this?
          Furthermore, with the database I had the possibility to change the header data.
          In addition, at this time I have some hidden column with have for example the color information in it, which I need.
          This color information isn't in the array.
          Is that with your way possible, too?

          the table should look something like that
          0_1541497400730_Unbenannt.PNG

          Sorry if this is basic.

          JonBJ 1 Reply Last reply
          0
          • M Mogli123

            @JonB
            Thank you for your help.

            No, it isn't necessary to save the data in a database.
            Yes, I have to save the data in a database.
            I tried it this way because that is the only way I know...

            @JonB :
            "You can tie a QTableView to a memory array without involving SQL/a database at all."
            Is that a "better and easier" way to manage my problem?
            How can I do this?
            Furthermore, with the database I had the possibility to change the header data.
            In addition, at this time I have some hidden column with have for example the color information in it, which I need.
            This color information isn't in the array.
            Is that with your way possible, too?

            the table should look something like that
            0_1541497400730_Unbenannt.PNG

            Sorry if this is basic.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #8

            @Mogli123
            Let's take a step back!

            I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?

            Forget Qt etc. for the moment. Do you or do you not wish to preserve that data in a database for some future purpose? If the answer turns out to be "No, I simply want to display the data in a table in the UI, when the program exits I'm happy for it to vanish" then you do not want to use a database/SQL! Instead look at http://doc.qt.io/qt-5/qstandarditemmodel.html, which is the basis for models which do not need SQL storage.

            M 1 Reply Last reply
            1
            • JonBJ JonB

              @Mogli123
              Let's take a step back!

              I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?

              Forget Qt etc. for the moment. Do you or do you not wish to preserve that data in a database for some future purpose? If the answer turns out to be "No, I simply want to display the data in a table in the UI, when the program exits I'm happy for it to vanish" then you do not want to use a database/SQL! Instead look at http://doc.qt.io/qt-5/qstandarditemmodel.html, which is the basis for models which do not need SQL storage.

              M Offline
              M Offline
              Mogli123
              wrote on last edited by Mogli123
              #9

              @JonB

              Tank you.

              *I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?

              Yes, exactly.
              I don't need the data in the future.
              I only have to display the data like in the picture, but not all of them. Some data I only need for the decision which color the row need, or other decisions. This data are at this time in hidden columns.

              JonBJ 1 Reply Last reply
              0
              • M Mogli123

                @JonB

                Tank you.

                *I think you are getting some data, which arrives in an array to your program, from some external source, which maybe sends new data every so often. Is that right?

                Yes, exactly.
                I don't need the data in the future.
                I only have to display the data like in the picture, but not all of them. Some data I only need for the decision which color the row need, or other decisions. This data are at this time in hidden columns.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #10

                @Mogli123
                So like I said: get rid of all SQL stuff, start out from QStandardItemModel (can be sub-classed if required), use that as your QTableView's model. (I said earlier you could use an array/QVector/whatever directly; I think you actually want to copy your data into a QStandardItemModel.) You can then insert/delete/update rows as your data arrives, and the view will update correspondingly.

                M 1 Reply Last reply
                0
                • JonBJ JonB

                  @Mogli123
                  So like I said: get rid of all SQL stuff, start out from QStandardItemModel (can be sub-classed if required), use that as your QTableView's model. (I said earlier you could use an array/QVector/whatever directly; I think you actually want to copy your data into a QStandardItemModel.) You can then insert/delete/update rows as your data arrives, and the view will update correspondingly.

                  M Offline
                  M Offline
                  Mogli123
                  wrote on last edited by Mogli123
                  #11

                  @JonB
                  Thank you very much for your help.

                  I create my own model like shown in my code above.
                  Is this with the QStandartItemModel possible on the same way?
                  I need that for manipulating the data.

                  How can I insert the data from my array (there are only qint32 in it) and how are they updating?
                  Whit signal and slots?

                  I know that are many questions and I'm very thankful for your help.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mogli123
                    wrote on last edited by Mogli123
                    #12

                    My boss just told me that he wants the data in a database.
                    He knows that it is not necessary at the moment, but later he needs the functions of a database.
                    So back to my original question, how can I update the database and the tableview?

                    I'm so sorry for this wrong information.

                    JonBJ 1 Reply Last reply
                    0
                    • M Mogli123

                      My boss just told me that he wants the data in a database.
                      He knows that it is not necessary at the moment, but later he needs the functions of a database.
                      So back to my original question, how can I update the database and the tableview?

                      I'm so sorry for this wrong information.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #13

                      @Mogli123
                      So then I said earlier:

                      You should update the model (QSqlTableModel, your TableCustomise) tied to the view with whatever data (e.g. http://doc.qt.io/qt-5/qsqltablemodel.html#setData, http://doc.qt.io/qt-5/qsqltablemodel.html#insertRecord) then the view will redraw to reflect that.

                      You need to read those, look at examples etc.

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        Mogli123
                        wrote on last edited by Mogli123
                        #14

                        Can you give me a smal example pleas how I have to do that?
                        That will be very helpfull.

                        Thank you very much.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mogli123
                          wrote on last edited by
                          #15

                          @JonB
                          Thank you for your help:)
                          I tried it like this and it seems that it works

                          cpp
                          ...
                            myTableCustomiseToolChange->setEditStrategy(QSqlTableModel::OnFieldChange);
                          ...
                          //Methoden
                          ...
                          void test_screen::functionFuerTestwert()
                          {
                              arrayCustomiseToolChange[1] ++;
                              myTableCustomiseToolChange->setData(myTableCustomiseToolChange->index(1,2), arrayCustomiseToolChange[1]);
                          }
                          

                          is that the right way?

                          JonBJ 1 Reply Last reply
                          0
                          • M Mogli123

                            @JonB
                            Thank you for your help:)
                            I tried it like this and it seems that it works

                            cpp
                            ...
                              myTableCustomiseToolChange->setEditStrategy(QSqlTableModel::OnFieldChange);
                            ...
                            //Methoden
                            ...
                            void test_screen::functionFuerTestwert()
                            {
                                arrayCustomiseToolChange[1] ++;
                                myTableCustomiseToolChange->setData(myTableCustomiseToolChange->index(1,2), arrayCustomiseToolChange[1]);
                            }
                            

                            is that the right way?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #16

                            @Mogli123
                            Yes, you seem to be using the model's setData() to alter values, which should be automatically reflected in the view, as per your original question.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mogli123
                              wrote on last edited by
                              #17

                              @JonB
                              Thank you very much:)

                              Have a nice day.

                              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