'[C++][ QT ] is not meant to be copied. Pass it by move instead
I am a 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 setQuery but in first argument, I set an object with *QSqlQuery type. And this error show up.
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";
}
}
Solution 1:[1]
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 delete it.
Alternatively, since the QSqlQuery
object is not used anywhere else, it might be best to chose the other overload for setQuery
like this:
model = new QSqlQueryModel();
model->setQuery("SELECT * FROM Prices", DB);
ui->tableView->setModel(model);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |