Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?


  • Lifetime Qt Champion

    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.



  • @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.



  • 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("");


  • Lifetime Qt Champion

    @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.



  • @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¢ :)


  • Lifetime Qt Champion

    @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.



  • @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?


  • Lifetime Qt Champion

    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.


Log in to reply