Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. Qt Contribution
  4. How is QSqlTableModel ok?
Forum Update on Monday, May 27th 2025

How is QSqlTableModel ok?

Scheduled Pinned Locked Moved Unsolved Qt Contribution
3 Posts 2 Posters 963 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.
  • V Offline
    V Offline
    VRonin
    wrote on 18 Nov 2021, 19:02 last edited by
    #1

    So here is my issue: the documentation for QSqlDatabase has a pretty big warning:

    Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

    QSqlTableModel explicitly goes against that warning: stores the db and never takes care of deleting it safely.

    How is it ok? Am I missing some magic somewhere?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Nov 2021, 19:56 last edited by
      #2

      Hi,

      On the deleting part, the model shall be deleted before the QCoreApplication and thus its private part as well and thus the db object.

      As for why they did it like that, it's rather a question for the original developer.

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

      V 1 Reply Last reply 19 Nov 2021, 09:41
      0
      • S SGaist
        18 Nov 2021, 19:56

        Hi,

        On the deleting part, the model shall be deleted before the QCoreApplication and thus its private part as well and thus the db object.

        As for why they did it like that, it's rather a question for the original developer.

        V Offline
        V Offline
        VRonin
        wrote on 19 Nov 2021, 09:41 last edited by
        #3

        @SGaist said in How is QSqlTableModel ok?:

        the model shall be deleted before the QCoreApplication and thus its private part as well and thus the db object.

        Not necessarily... for example

        #include <QApplication>
        #include <QTableView>
        #include <QSqlTableModel>
        #include <memory>
        int main(int argc, char *argv[])
        {
            std::unique_ptr<QSqlTableModel> model;
            QApplication a(argc, argv);
            QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
            db.setDatabaseName(QStringLiteral("/path/to/mydb.sqlite"));
            Q_ASSUME(db.open());
            model = std::make_unique<QSqlTableModel>();
            model->setTable(QStringLiteral("MyTable"));
            Q_ASSUME(model->select());
            QTableView tableView;
            tableView.setModel(model.get());
            tableView.show();
            return a.exec();
        }
        

        Now we can agree that, in practice, it seldom happens but I still don't understand if the design choice opens up the problem without safeguards or warnings or if I'm just missing something that makes it ok.

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0

        3/3

        19 Nov 2021, 09:41

        • Login

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