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. What is the cleanest way to clear a selection in a QSqlTableModel?
Qt 6.11 is out! See what's new in the release blog

What is the cleanest way to clear a selection in a QSqlTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.7k 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    I have the following code:

    QSqlTablemodel *user = new QSqlTableModel(this);
    user->setTable("user");
    user->setFilter("userId='1');
    user->select();
    

    Now I would like to clear the selection. What is the cleanest way to do this?

    I did the following:

    user->setFilter("userId='0');
    user->select();
    

    Is this correct or is there a cleaner way to do this?

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

      Hi,

      What kind of selection do you have in mind ?

      Because you are currently filtering your model while selection rather related to the view showing your model data.

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

      I 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What kind of selection do you have in mind ?

        Because you are currently filtering your model while selection rather related to the view showing your model data.

        I Offline
        I Offline
        Infinity
        wrote on last edited by
        #3

        @SGaist The model holds the data of a logged in user. When the user logs out I want to clear all the user data from the model. I'm not quite sure about what the cleanest way is to clear the model.

        1 Reply Last reply
        0
        • rrlopezR Offline
          rrlopezR Offline
          rrlopez
          wrote on last edited by rrlopez
          #4

          Hi, have you seen the "clear" function of the QSqlTableModel class?
          https://doc.qt.io/qt-5/qsqltablemodel.html#clear
          You can as well try to clear the users model by setting an empty filter:
          user->setFilter("");

          Lic-Ing. Rodrigo Lopez Gonzalez
          Embedded Software Engineer
          RidgeRun Engineering Ltd.
          www.ridgerun.com
          Email: rodrigo.lopez@ridgerun.com

          SGaistS 1 Reply Last reply
          1
          • rrlopezR rrlopez

            Hi, have you seen the "clear" function of the QSqlTableModel class?
            https://doc.qt.io/qt-5/qsqltablemodel.html#clear
            You can as well try to clear the users model by setting an empty filter:
            user->setFilter("");

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @rrlopez an empty filter would just show everything available in the table.

            One way would be to set an empty table name so it's not even loaded with any data.

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

            JonBJ 1 Reply Last reply
            0
            • SGaistS SGaist

              @rrlopez an empty filter would just show everything available in the table.

              One way would be to set an empty table name so it's not even loaded with any data.

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

              @SGaist said in What is the cleanest way to clear a selection in a QSqlTableModel?:

              One way would be to set an empty table name so it's not even loaded with any data.

              Although of course I recognize you are the expert, this is plain wicked! If QSqlTableModel::setTable(const QString &tableName) accepts "" for tableName then docs should say so, which they don't. Otherwise I'd be worried about behaviour/error (" Does not select data from the table, but fetches its field information."). Just IMHO/my 2ยข :)

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

                @JonB good and valid point. Personally I would just expect to have an error in the model itself. You could be passing a wrong table name which should trigger pretty much the same behaviour as you can't fetch what does not exist.

                In any case, I must say that if I would not necessarily use that kind of design client side but since I don't know what this is about I can't comment any further.

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

                I 1 Reply Last reply
                0
                • SGaistS SGaist

                  @JonB good and valid point. Personally I would just expect to have an error in the model itself. You could be passing a wrong table name which should trigger pretty much the same behaviour as you can't fetch what does not exist.

                  In any case, I must say that if I would not necessarily use that kind of design client side but since I don't know what this is about I can't comment any further.

                  I Offline
                  I Offline
                  Infinity
                  wrote on last edited by
                  #8

                  @SGaist I want to achieve the following:

                  The model is connected to a tableView

                  1. Clear all records from the model which also clears the tableView
                  2. Get an empty record from user->record();
                  3. Set the values in the new record
                  4. Insert the new record in the model and database which will also show the new record in the tableView.

                  My question is how to clear the model that it doesn't contain any data, but I still want to get an empty record from it. I imagine a function like user->clearAllData(), but this function obviously doesn't exist. Now I would like to subclass QSqlTableModel and create such a function. How would you write this function?

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

                    Something to define clearly: what does clear mean for you ? Just empty the view or also nuke the data in the database ?

                    From the looks of it, it seems that you should rather load the database data inside e.g. a QStandardItemModel and then only contact the database on demand.

                    But since I am not sure about what you are trying to achieve, I may be wrong.

                    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

                    • Login

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