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. QTableWidget and SQL (dynamic) not working

QTableWidget and SQL (dynamic) not working

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetsqlsqlitequery
12 Posts 3 Posters 6.4k 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.
  • SGaistS SGaist

    Hi,

    You don't add any row to your QTableWidget or did you do before running that loop ?

    Also, why not use a QTableView with QSqlQueryModel ?

    cxamC Offline
    cxamC Offline
    cxam
    wrote on last edited by
    #3

    @SGaist "i" is the number of rows that add's +1 at the end of the loop

    Stay Hungry, Stay Foolish

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      You don't add any row to your QTableWidget or did you do before running that loop ?

      Also, why not use a QTableView with QSqlQueryModel ?

      cxamC Offline
      cxamC Offline
      cxam
      wrote on last edited by
      #4

      @SGaist But how QtableView works ?

      Stay Hungry, Stay Foolish

      Joel BodenmannJ 1 Reply Last reply
      0
      • cxamC cxam

        @SGaist But how QtableView works ?

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #5

        I can recommend reading these two articles to not only understand the model/view concept but also how Qt implements it:

        • https://doc.qt.io/qt-5/model-view-programming.html
        • https://doc.qt.io/qt-5/modelview.html

        As @SGaist pointed out there's an existing model that allows you to display SQL query results in a QTableView. The model is called QSqlQueryModel. You'd basically use a QTableView instead of a QTableWidget and use QTableView::setModel() to set the QSqlQueryModel as the model for the table view.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        cxamC 1 Reply Last reply
        0
        • Joel BodenmannJ Joel Bodenmann

          I can recommend reading these two articles to not only understand the model/view concept but also how Qt implements it:

          • https://doc.qt.io/qt-5/model-view-programming.html
          • https://doc.qt.io/qt-5/modelview.html

          As @SGaist pointed out there's an existing model that allows you to display SQL query results in a QTableView. The model is called QSqlQueryModel. You'd basically use a QTableView instead of a QTableWidget and use QTableView::setModel() to set the QSqlQueryModel as the model for the table view.

          cxamC Offline
          cxamC Offline
          cxam
          wrote on last edited by
          #6

          @Joel-Bodenmann I tried to use QTableView but actually it would break a lot of my code so unless it's completely necessary I won't use it. I'm focusing on trying to solve my current issue.

          Stay Hungry, Stay Foolish

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

            What would break ?

            In any case, I don't see you creating new rows. The incrementation of i doesn't mean your QTableWidget will have an additional row unless you did it before your while loop.

            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
            0
            • cxamC cxam

              @Joel-Bodenmann I tried to use QTableView but actually it would break a lot of my code so unless it's completely necessary I won't use it. I'm focusing on trying to solve my current issue.

              Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by Joel Bodenmann
              #8

              @cxam QTableWidget actually inherits from QTableView so it shouldn't break anything in terms of the GUI. In terms of the data handling: You just outsource that to the model. As there's an existing model that is designed specifically for handling SQL queries I can imagine that the only thing it would do is drastically simplify your life.
              The QTableWidget really is just a QTableView with an internal model. That model is a generic model. Using the QTableView would allow you to use the SQL query specific QSqlQueryModel.

              @SGaist QTableWidget::setItem() actually creates the row internally.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              cxamC 1 Reply Last reply
              1
              • Joel BodenmannJ Joel Bodenmann

                @cxam QTableWidget actually inherits from QTableView so it shouldn't break anything in terms of the GUI. In terms of the data handling: You just outsource that to the model. As there's an existing model that is designed specifically for handling SQL queries I can imagine that the only thing it would do is drastically simplify your life.
                The QTableWidget really is just a QTableView with an internal model. That model is a generic model. Using the QTableView would allow you to use the SQL query specific QSqlQueryModel.

                @SGaist QTableWidget::setItem() actually creates the row internally.

                cxamC Offline
                cxamC Offline
                cxam
                wrote on last edited by
                #9

                @Joel-Bodenmann Hmm I see. I'll check it out then, thanks for your help.

                Stay Hungry, Stay Foolish

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

                  @Joel-Bodenmann No it doesn't. First thing done, it checks whether the row, column combination returns a valid index if not then it returns immediately.

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

                  Joel BodenmannJ cxamC 2 Replies Last reply
                  0
                  • SGaistS SGaist

                    @Joel-Bodenmann No it doesn't. First thing done, it checks whether the row, column combination returns a valid index if not then it returns immediately.

                    Joel BodenmannJ Offline
                    Joel BodenmannJ Offline
                    Joel Bodenmann
                    wrote on last edited by
                    #11

                    @SGaist Yes, you are correct. I am sorry for giving wrong information.

                    @cxam So as @SGaist pointed out in his first post: Make sure that your QTableWidget "has enough rows".
                    But anyway, I'd still recommend going with QTableView and QSqlQueryModel in your case.

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @Joel-Bodenmann No it doesn't. First thing done, it checks whether the row, column combination returns a valid index if not then it returns immediately.

                      cxamC Offline
                      cxamC Offline
                      cxam
                      wrote on last edited by
                      #12

                      @SGaist Indeed, you're right When I set the properties for my table I set that the number of rows was "1" so I have to create an "int" variable and then say that the number of rows is that variable starting from 1 and then increment that number on the loop.

                      Stay Hungry, Stay Foolish

                      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