Home > Net >  [C ][ QT ] is not meant to be copied. Pass it by move instead
[C ][ QT ] is not meant to be copied. Pass it by move instead

Time:11-30

Hello i'am beginner in c . And i don't understand this error. I just need you to explain me. I try to show a .sqlite database in a QTableview. The problem come from:

model->setQuery(*qry);

i want to use a function called setQuerry but in first argument, i set an object with *QSqlQuery type. And this error show up.

ERROR Pics

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlDatabase>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QSqlDatabase DB;
    QSqlQueryModel* model;

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;

    DB = QSqlDatabase::addDatabase("QSQLITE");
    DB.setDatabaseName("Prices.sqlite");

    if (DB.open()) {
        qDebug() << "Open";

        model = new QSqlQueryModel();

        QSqlQuery* qry = new QSqlQuery(DB);
        qry->prepare("SELECT * FROM Prices");
        qry->exec();

        model->setQuery(*qry);
        ui->tableView->setModel(model);

        qDebug() << "Rows: " << model->rowCount();

        DB.close();
    }
    else {
        qDebug() << "Failed connection";
    }
}


CodePudding user response:

They want you to move the object behind qry into the function.

The shortest change would be to replace

model->setQuery(*qry);

with

model->setQuery(std::move(*qry));
delete qry;

You don't need to use new/delete in this case though. Just using automatic storage duration works:

    QSqlQuery qry(DB);
    qry.prepare("SELECT * FROM Prices");
    qry.exec();
    model->setQuery(std::move(qry));

Then you don't have to worry about forgetting to dele it.

  • Related