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. [SOLVED] Need some guidance on how to use QSortfilterproxymodel with QSqltablemodel to display checkboxes and perform filter operation
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Need some guidance on how to use QSortfilterproxymodel with QSqltablemodel to display checkboxes and perform filter operation

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 4.2k 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.
  • R Offline
    R Offline
    roahanramesh
    wrote on last edited by
    #1

    Hello
    I am new to Qt Programming, just started about a month ago. I have tried to get some data and I am unable to comprehend the subject.

    I am currently using a MySQL database to store my data. I am using a QSqlTableModel to get data from this model to display this in a QTableView.

    However, I need to implement the following functionalities.

    1. Provide a checkbox on each row in column 0 (Depending on whether the checkbox is selected, I will populate a couple of lineedits for editing. On submit, I will update the database, which inturn will update the model then the view.

    2. I also need to implement a filter scheme, wherein on typing a string, the data in the view should update automatically. I will use a qlineedit that emits on textedited signal to accomplish this.

    Please let me know if I need to use a delegate or can I use a QSortFilterProxyModel to accomplish this task.

    Thanks for your help. Also I apologize for the inconvenience.

    Regards

    Roahan

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      For the filtering, yes you can use QSortFilterProxyModel or even subclass it, if you need more control over the filtering (e.g. just reimplement filterAcceptsRow with the condition you need).

      The checkbox and lineedit part is a bit more tricky. You see, a view usually just shows what the model contains. When I undestand you correctly, you want the view to show something (checkboxes) that doesn't exist in the model at all, and something (the row with the lineedits) that doesn't exist yet. All this will require you to modify either the underlying model class (by making it provide an extra bool column for the checkboxes and some empty values for newly added lines - this assumes your sql database can handle temporarily empty lines, without breaking other applications dependant on the database, e.g. dynamically created web content), or the view class.
      For the checkboxes/ -lineedits- you should indeed use QStyledItemDelegate (QItemDelegate is deprecated).
      //EDIT: You probably won't need lineedits if you allow empty new rows in the model. The Item Views automatically create editable cells for you, when you include Qt::ItemIsEditable in the returned flag for that index.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roahanramesh
        wrote on last edited by
        #3

        thanks for your tip. will try out what was proposed.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          There is sample code available for the checkboxes. There are two solutions available in the wiki: "one":/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns that displays a checkbox that represents a boolean field in your underlying model (so, instead of a true/false, you get a checkbox), and "another":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox that adds a checkbox without there being a field for it in the underlying model (more suitable for extended selections, for instance). I wrote the second one.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roahanramesh
            wrote on last edited by
            #5

            thanks andre will go ahead with your code, since it will be a good for fit my requirement. but one question remains. I need to implement a filter mechanism which is an either by memid (member id) or by full name. should I re -implement filteracceptsrow . Your thoughts !!!

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              [quote author="Andre" date="1336563355"]There is sample code available for the checkboxes. There are two solutions available in the wiki: "one":/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns that displays a checkbox that represents a boolean field in your underlying model (so, instead of a true/false, you get a checkbox), and "another":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox that adds a checkbox without there being a field for it in the underlying model (more suitable for extended selections, for instance). I wrote the second one.
              [/quote]

              Andre, I'm also new to Qt and I'm trying to do something similar to roahanramesh, I can have a bool field in my database which I think I'd prefer to do but don't have to. But how do I empliment either of these examples? I cant figure out where I would put them in my code or what I need to change to make it work with mine or anything.

              Can you help me out?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                roahanramesh
                wrote on last edited by
                #7

                Hello
                Can you post a snippet, so I can be of some help.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  a snippet of what part of my code?

                  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