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. How to resolve this warning
Forum Updated to NodeBB v4.3 + New Features

How to resolve this warning

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

    Hi, I executed a query to display a list from the database. However, the model->setQuery(*qry) shows a warning

    'setquery' is deprecated. QSqlQuery is not meant to be copied. Pass it my move instead.

    How should I resolve this warning?

    db = QSqlDatabase::database();
        QSqlQueryModel * model = new QSqlQueryModel();
        QSqlQuery *qry = new QSqlQuery(db);
        qry->prepare("select * from plan order by plandet desc");
        qry->exec();
        model->setQuery(*qry);
        delete qry;
        int rowcount = model->rowCount();
        qDebug()<< rowcount;
    
        ui->listView->setModel(model);
        db.close();
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't create the query on the heap but on the stack and then move it to setQuery()

      model->setQuery(std::move(qry))

      See also https://doc.qt.io/qt-6/qsqlquerymodel-obsolete.html#setQuery-1

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ J.HilkJ 2 Replies Last reply
      2
      • B burrito1111

        Hi, I executed a query to display a list from the database. However, the model->setQuery(*qry) shows a warning

        'setquery' is deprecated. QSqlQuery is not meant to be copied. Pass it my move instead.

        How should I resolve this warning?

        db = QSqlDatabase::database();
            QSqlQueryModel * model = new QSqlQueryModel();
            QSqlQuery *qry = new QSqlQuery(db);
            qry->prepare("select * from plan order by plandet desc");
            qry->exec();
            model->setQuery(*qry);
            delete qry;
            int rowcount = model->rowCount();
            qDebug()<< rowcount;
        
            ui->listView->setModel(model);
            db.close();
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @burrito1111 Do you use Qt6? See the signature: https://doc-snapshots.qt.io/qt6-dev/qsqlquerymodel.html#setQuery
        You need to do it like this:

        QSqlQuery qry(db); // Do NOT allocate on the heap
        model->setQuery(std::move(qry));
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          Don't create the query on the heap but on the stack and then move it to setQuery()

          model->setQuery(std::move(qry))

          See also https://doc.qt.io/qt-6/qsqlquerymodel-obsolete.html#setQuery-1

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

          @Christian-Ehrlicher, @jsulm

          Don't create the query on the heap but on the stack and then move it to setQuery()

          model->setQuery(std::move(qry))

          QSqlQuery qry(db); // Do NOT allocate on the heap

          I wonder how Python/PyQt/PySide (everything is on the heap) handles this then? :)

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @Christian-Ehrlicher, @jsulm

            Don't create the query on the heap but on the stack and then move it to setQuery()

            model->setQuery(std::move(qry))

            QSqlQuery qry(db); // Do NOT allocate on the heap

            I wonder how Python/PyQt/PySide (everything is on the heap) handles this then? :)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB Well, you can allocate on the heap, but after moving you will need to delete it, else you will have a memory leak.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Christian EhrlicherC 1 Reply Last reply
            1
            • jsulmJ jsulm

              @JonB Well, you can allocate on the heap, but after moving you will need to delete it, else you will have a memory leak.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @jsulm said in How to resolve this warning:

              but after moving you will need to delete it

              Which will result in a crash - a moved-out object is in an undefined state. And even if not it would call the QSqlQuery dtor and kill the moved object too.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              jsulmJ 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @jsulm said in How to resolve this warning:

                but after moving you will need to delete it

                Which will result in a crash - a moved-out object is in an undefined state. And even if not it would call the QSqlQuery dtor and kill the moved object too.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #7

                @Christian-Ehrlicher said in How to resolve this warning:

                and kill the moved object too

                Are you sure about that?
                If the move constructor is implemented properly the other QSqlQuery object should not be affected by destroying the original (moved) one. The destructor is also called when the stack allocated object goes out of scope.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  Don't create the query on the heap but on the stack and then move it to setQuery()

                  model->setQuery(std::move(qry))

                  See also https://doc.qt.io/qt-6/qsqlquerymodel-obsolete.html#setQuery-1

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher that's a stupid requirement to have, only God knows if the compiler will actually move something or not :D

                  It's really all just a suggestion, same as inline


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • B burrito1111

                    Hi, I executed a query to display a list from the database. However, the model->setQuery(*qry) shows a warning

                    'setquery' is deprecated. QSqlQuery is not meant to be copied. Pass it my move instead.

                    How should I resolve this warning?

                    db = QSqlDatabase::database();
                        QSqlQueryModel * model = new QSqlQueryModel();
                        QSqlQuery *qry = new QSqlQuery(db);
                        qry->prepare("select * from plan order by plandet desc");
                        qry->exec();
                        model->setQuery(*qry);
                        delete qry;
                        int rowcount = model->rowCount();
                        qDebug()<< rowcount;
                    
                        ui->listView->setModel(model);
                        db.close();
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @burrito1111
                    Going all the way back to your original code. Can't you get rid of your need to explicitly create a QSqlQuery by just having the whole thing written as:

                    QSqlQueryModel * model = new QSqlQueryModel();
                    model->setQuery("select * from plan order by plandet desc");
                    int rowcount = model->rowCount();
                    ...
                    

                    ? Much simpler, avoids all this stuff, new/deleteing too. Are you trying to ensure the qry->prepare() gets done? (I haven't looked at whether model->setQuery(QString) does that anyway.)

                    1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4 referenced this topic on

                    • Login

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