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. Selecting a database while using QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Selecting a database while using QSqlTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
sqldatabaseqsqltablemodel
8 Posts 3 Posters 3.9k Views 3 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.
  • S Offline
    S Offline
    spektro37
    wrote on last edited by spektro37
    #1

    I am going through the Qt documentation related to working with MySQL:
    http://doc.qt.io/qt-5/sql-model.html

    I have managed to run QQuery and retrieve some data from my test database, but when I tried to follow instructions in the Using the SQL Model Classes section, I didn't get any result. The example I have adapted to my database is:

        QSqlTableModel model;
        model.setTable("employee");
        model.setFilter("salary > 50000");
        model.setSort(2, Qt::DescendingOrder);
        model.select();
    
        for (int i = 0; i < model.rowCount(); ++i) {
            QString name = model.record(i).value("name").toString();
            int salary = model.record(i).value("salary").toInt();
            qDebug() << name << salary;
        }
    

    Adapted version:

        QSqlTableModel model;
        model.setTable("groups");
        model.select();
    
        for (int i = 0; i < model.rowCount(); ++i) {
            int groupNumber = model.record(i).value("number").toInt();
            cout << groupNumber << endl;
        }
    

    I am trying to select the groups table with only one column (number) (which holds primary keys). I have established connections with two databases, named live and arch, and would like to use one of them for this task.

    I have googled for any information related to this topic, but had no luck. Could someone please give advice and guidance? It would be much appreciated.

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

      Hi and welcome to devnet,

      You can select the database when constructing your QSqlTableModel object. See the QSqlTableModel documentation.

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

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        You can select the database when constructing your QSqlTableModel object. See the QSqlTableModel documentation.

        S Offline
        S Offline
        spektro37
        wrote on last edited by
        #3

        @SGaist Hi, thank you for your reply and 'welcome'. I have tried to follow the QSqlTableModel documentation you have provided the link to. I am struggling to understand what should I provide as the first argument (parent). Could you please advise? This is the code I have used:

        //Name of the connection I want to use
        QString database = "arch";
        //Declaring a pointer to a QObject to use as the parent (have no idea what should it be)
        QObject *parent;
        //Creating a QSqlDatabase object to pass as the database to the QSqlTableModel constructor
        QSqlDatabase db = QSqlDatabase::database(database);
        //Creating a pointer to a new QSqlTableModel object
        QSqlTableModel *model = new QSqlTableModel(parent, db);
        //Trying to get information about the selected database (doesn't return anything in this case)
        qDebug() << model->database();
        //Selecting the "groups" table
        model->setTable("groups");
        model->select();
        //Trying to list all entries of the table (doesn't output anything in this case)
        for(int i = 0; i < model->rowCount(); ++i)
        {
        	int groupNumber = model->record(i).value("number").toInt();
        	cout << groupNumber << endl;
        }
        
        mrjjM 1 Reply Last reply
        0
        • S spektro37

          @SGaist Hi, thank you for your reply and 'welcome'. I have tried to follow the QSqlTableModel documentation you have provided the link to. I am struggling to understand what should I provide as the first argument (parent). Could you please advise? This is the code I have used:

          //Name of the connection I want to use
          QString database = "arch";
          //Declaring a pointer to a QObject to use as the parent (have no idea what should it be)
          QObject *parent;
          //Creating a QSqlDatabase object to pass as the database to the QSqlTableModel constructor
          QSqlDatabase db = QSqlDatabase::database(database);
          //Creating a pointer to a new QSqlTableModel object
          QSqlTableModel *model = new QSqlTableModel(parent, db);
          //Trying to get information about the selected database (doesn't return anything in this case)
          qDebug() << model->database();
          //Selecting the "groups" table
          model->setTable("groups");
          model->select();
          //Trying to list all entries of the table (doesn't output anything in this case)
          for(int i = 0; i < model->rowCount(); ++i)
          {
          	int groupNumber = model->record(i).value("number").toInt();
          	cout << groupNumber << endl;
          }
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @spektro37 said in Selecting a database while using QSqlTableModel:

          QObject *parent;

          Hi
          That is not ok. it will be a dangling pointer.
          Im not 100% sure why it needs the parent for, but if the normal usage ( auto deletion), you can safely use nullptr and then do
          delete model; when no longer needed.
          Or if inside main window or widget , simply use "this" pointer.
          ( and no delete on your own)

          S 1 Reply Last reply
          1
          • mrjjM mrjj

            @spektro37 said in Selecting a database while using QSqlTableModel:

            QObject *parent;

            Hi
            That is not ok. it will be a dangling pointer.
            Im not 100% sure why it needs the parent for, but if the normal usage ( auto deletion), you can safely use nullptr and then do
            delete model; when no longer needed.
            Or if inside main window or widget , simply use "this" pointer.
            ( and no delete on your own)

            S Offline
            S Offline
            spektro37
            wrote on last edited by
            #5

            @mrjj Hi. Thank you for your advice, everything is working now. I should have known better, than declaring an uninitialised pointer.

            It would still be good to know why a parent is required, though. I hope someone would be able to shed some light.

            mrjjM 1 Reply Last reply
            0
            • S spektro37

              @mrjj Hi. Thank you for your advice, everything is working now. I should have known better, than declaring an uninitialised pointer.

              It would still be good to know why a parent is required, though. I hope someone would be able to shed some light.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @spektro37
              Super :)
              My best guess is like for all the other widgets,

              http://doc.qt.io/qt-5/objecttrees.html

              So its owned by say mainwindow and it will delete it when
              it self are deleted. (just like buttons etc)

              S 1 Reply Last reply
              1
              • mrjjM mrjj

                @spektro37
                Super :)
                My best guess is like for all the other widgets,

                http://doc.qt.io/qt-5/objecttrees.html

                So its owned by say mainwindow and it will delete it when
                it self are deleted. (just like buttons etc)

                S Offline
                S Offline
                spektro37
                wrote on last edited by
                #7

                @mrjj
                It does make sense, if you put it that way.)

                Very useful article! Thanks for sharing.)

                mrjjM 1 Reply Last reply
                0
                • S spektro37

                  @mrjj
                  It does make sense, if you put it that way.)

                  Very useful article! Thanks for sharing.)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @spektro37
                  Well its used with all QWidgets so would make sense.

                  Its something to be aware of when creating GUI and also the little note that
                  any QWidget that are not given a owner/parent, will become a window.
                  That can be surprised when coming from other frameworks.

                  Say you make a new label

                  QLabel *lab= new QLabel();
                  

                  Since its given no parent, it will become a window.

                  That was pretty surprising to me first time as i wanted to insert it into the main window.

                  So its good to know about.

                  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