QSqlTableModel inserting new record, getting data is QVariant(invalid) - qt

I have a simple QSqlTableModel:
class UsersModel : public QSqlTableModel
{
Q_OBJECT
public:
UsersModel();
~UsersModel();
bool newUser(const QString &name, const QString &surname, const QString &birthday);
};
UsersModel::UsersModel()
{
setTable("users");
select();
}
When I'm inserting a new record into the model, it is inserted into the database, but immediate retrieving of this record from the model doesn't work:
bool UsersModel::newUser(const QString &name, const QString &surname, const QString &birthday)
{
QSqlRecord rec = record();
rec.setGenerated("id", true);
rec.setValue("name", QVariant(name));
rec.setValue("surname", QVariant(surname));
rec.setValue("birthday", QVariant(birthday));
bool res = insertRecord(-1, rec);
if (res) {
qDebug() << "last inserted id:" << query().lastInsertId();
qDebug() << "name: " << record(rowCount() - 1).value("name");
}
return res;
}
Result of newUser method call:
last inserted id: QVariant(qlonglong, 31)
name: QVariant(Invalid)
If I look into the database, record is inserted, but if I try to get it from the model, invalid QVariant is returned.
What I'm doing wrong? Shouldn't the model return data for the newly inserted record?

Related

Mime type for custom data in tree view

The items in the tree view hold a instance of class container.
I want to implement drag and drop functionality in the view.
According to the QT tutorial for the data to copy i need specify the mime type and than write the Mimedata and dropMimeData functions.
The QT Example is dealing with a simple string so i am totally clueless of how to implement these function in case of custom objects.
1) What should be the mime type in my case ?
2) How to implement the current mimedata function for Container object data?
3) How to implement the current dropmimedata function for Container object data?
/////////////////////////////////////////
class Container
{
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
int iSegments;
float fRadius;
public:
Container();
Container(std::string , std::string , int , float);
Container(const Container& obj);
~Container();
std::string GetName();
std::string GetType();
void SetName(std::string stdstrName);
};
Q_DECLARE_METATYPE( Container )
////////////////////////////////////////////////////////////
QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes)
const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
QString text = data(index, Qt::DisplayRole).toString();
// I have a GetContainer function which returns the Container
//object and i can use the GetContainer instead of data function.
stream << text;
}
}
mimeData->setData("application/vnd.text.list", encodedData);
return mimeData;
}
//////////////////////////////////////////////////////////////////
bool DragDropListModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex
&parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("application/vnd.text.list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
QByteArray encodedData = data->data("application/vnd.text.list");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QStringList newItems;
int rows = 0;
while (!stream.atEnd()) {
QString text;
stream >> text;
newItems << text;
++rows;
}
insertRows(beginRow, rows, QModelIndex());
foreach (const QString &text, newItems) {
QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, text);
beginRow++;
}
return true;
}
The header file for TreeItem.
class TreeItem
{
public:
explicit TreeItem( const Container &data , TreeItem *parent = 0 );
~TreeItem();
TreeItem *parent();
void appendChild(TreeItem *child);
TreeItem *child(int iNumber);
int childCount() const;
int childNumber() const;
Container data() const;
bool setData(const Container &data , QVariant value);
void setContainer(const Container &data);
bool insertChildren(int position, int count );
bool removeChildren( int position , int count );
private:
QList<TreeItem*> childItems;
Container itemData;
TreeItem* parentItem;
}
You can add your custom mime types to specify the type of container you want to drag/drop. See this post for details.
The QDrag object constructed by the source contains a list of MIME types that it uses to represent the data (ordered from most appropriate to least appropriate), and the drop target uses one of these to access the data.
First of all, try to find a compatible standard mime type. Those are the most common one assigned by the IANA.
If the one you are looking for is not in the list, then you can label your custom one and serialize your data into a QByteArray to share it.
QByteArray output;
// do whatever
mimeData->setData("my-awesome-mime-type", output);
Now, in your custom widget, don't forget to accept the drops of this mime type:
void Window::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("my-awesome-mime-type"))
event->acceptProposedAction();
}
You can find a complete example in this project.

How to declare a negative number on a QTableView using SQlite

I am designing a major user interface with several fields.
In order to shrink the problem I created a small minimal application with 5 columns: name, image, dataDatabase, dateTime and coordNumber.
I have a MainWindow with a QTableView, as soon as I right click inside the QTableView an AddItemDialog opens up with:
1) nameLineEdit
2) ImLineEdit
3) imageLineEdit
4) dateTimeEdit
5) numLineEdit
The issue that I have is that I can't find a way to accept the 5) numLineEdit through the AddItemDialog when the number is negative.
Currently it only saves positive numbers. How to handle this exception?
I read from official document but I could not figure out. However always from this official source it seems that a negative number must be interpreted as "no limit" value. The explanation was short and didn't provide any useful small example, so I am still not sure how to proceed.
I am including the most important parts of the application below with the related description of the procedure I followed:
I created an Item with the fields item.h:
class Item
{
public:
Item(const double dateTime,
const QString &name = "", const QString &image = "",
int num, const QByteArray &imagesData = QByteArray());
QString name() const { return mName; }
QString image() const { return mImage; }
QByteArray imagesData() const { return mImagesData; }
double dateTime() const { return mDateTime; }
int num() const { return mNumberCoord; }
private:
QString mName;
QString mImage;
QByteArray mImagesData;
double mDateTime;
int mNumberCoord
};
and its related item.cpp
Item::Item(const double dateTime, int num,
const QString &name, const QString &image, int num,
const QByteArray &imagesData) :
mName(name),
mImage(image),
mImagesData(imagesData),
mDateTime(dateTime),
mNumberCoord(num)
{
}
I created a database.h table that will contain the parameters as follows:
class dataBase : public QObject
{
Q_OBJECT
public:
explicit dataBase(QObject *parent = nullptr);
bool inizializationDataBase(const QString &nameDataBase);
bool configureDataBase();
QString getError() const { return mError; }
bool addItem(const Item &item);
private:
QSqlDatabase mDatabase;
QString mError;
};
And its related database.cpp file - I am only including the most important piece of the code for this file:
#define CREATE_TABLE \
" CREATE TABLE IF NOT EXISTS Fish_Table" \
" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
", name TEXT NOT NULL" \
", image TEXT NOT NULL" \
", dataDataBase BLOB NOT NULL" \
", dateTime DOUBLE NOT NULL" \
", num INTEGER NOT NULL)"
dataBase::dataBase(QObject *parent) : QObject(parent)
{
}
bool dataBase::addItem(const Item &item) {
QSqlQuery q;
q.prepare("INSERT INTO Fish_Table (name, image, dataDatabase, dateTime, num) VALUES (?,?,?,?,?)");
q.addBindValue(item.name());
q.addBindValue(item.image());
q.addBindValue(item.imagesData());
q.addBindValue(item.dateTime());
q.addBindValue(item.num());
bool ok = q.exec();
if (!ok) {
mError = q.lastError().text();
}
return ok;
}
and finally the AddItemDialog.cpp that contains the fields I am trying to pass to the QTableView of the MainWindow.
AddItemDialog.cpp
void AddItemDialog::on_buttonBox_accepted()
{
QFile dataBase(ui->imageLineEdit->text());
if (!dataBase.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, "Error", dataBase.errorString());
return;
}
mItem = Item(ui->dateTimeEdit->dateTime(),
ui->nameLineEdit->text(),
ui->ImLineEdit->text(),
ui->numLineEdit->text(),
dataBase.readAll());
dataBase.close();
accept();
}
I expect to save either positive or negative numbers on the QTableView, but as of now I can only save positive numbers. How to handle this exception?
So the answer is yes, it is possible to handle negative numbers. For some reasons I thought that SQL was not able to do that but below is a small example of how to handle that exception:
#define CREATE_TABLE \
" CREATE TABLE IF NOT EXISTS Fish_Table" \
" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
", name TEXT NOT NULL" \
", image TEXT NOT NULL" \
", dataDataBase BLOB NOT NULL" \
", dateTime DOUBLE NOT NULL" \
", num INTEGER NOT NULL)"
dataBase::dataBase(QObject *parent) : QObject(parent)
{
}
bool dataBase::addItem(const Item &item) {
QSqlQuery q;
q.prepare("INSERT INTO Fish_Table (name, image, dataDatabase, dateTime, num) VALUES (name,descriptionOfImage,BLOB(actual image), put date&time,-200)");
q.addBindValue(item.name());
q.addBindValue(item.image());
q.addBindValue(item.imagesData());
q.addBindValue(item.dateTime());
q.addBindValue(item.num());
bool ok = q.exec();
if (!ok) {
mError = q.lastError().text();
}
return ok;
}
This is also a very valuable source.
If also there is any need to turn a positive number into a negative number than this is a possible solution:
UPDATE Table
SET field= (field * -1)
Where SIGN(field) = -1
Also this is very useful in case someone needs the SQL code for an additional source.

Passing a QObject to an Script function with QJSEngine?

I'm trying to call a function in an external script while passing a QObject as a parameter.
My QObject is defined as this:
#ifndef INSERTVALUES_H
#define INSERTVALUES_H
#include <QObject>
struct insertValueDef
{
QString name;
QString xmlCode;
QString value;
bool key;
bool insert;
};
typedef insertValueDef TinsertValueDef;
class insertValues : public QObject
{
Q_OBJECT
public:
explicit insertValues(QObject *parent = 0);
~insertValues();
void insertValue(TinsertValueDef value);
int count();
void setItemName(int index, QString name);
void setItemXMLCode(int index, QString xmlCode);
void setItemValue(int index, QString value);
void setItemIsKey(int index, bool isKey);
void setItemToInsert(int index, bool toInsert);
QString itemName(int index);
QString itemXMLCode(int index);
QString itemValue(int index);
bool itemIsKey(int index);
bool itemToInsert(int index);
bool valueIsNumber(int index);
int getIndexByColumnName(QString name);
private:
QList<TinsertValueDef> m_insertList;
};
#endif // INSERTVALUES_H
My JS Script function is this:
function beforeInsert(table,data)
{
if (table == "tmpTable")
{
var index = data.getIndexByColumnName("tmpfield");
if (index >= 0)
{
data.setItemValue(index,"Carlos Quiros");
}
}
}
The code that runs runs the script is the following:
QFile scriptFile(javaScript);
if (!scriptFile.open(QIODevice::ReadOnly))
{
log("Error: Script file defined but cannot be opened");
return 1;
}
JSEngine.evaluate(scriptFile.readAll(), javaScript);
scriptFile.close();
insertValues insertObject;
TinsertValueDef tfield;
tfield.key = false;
tfield.name = "tmpfield";
tfield.xmlCode = "tmpCode";
tfield.value = "tmpValue";
tfield.insert = true;
insertObject.insertValue(tfield);
QString error;
beforeInsertFunction = JSEngine.evaluate("beforeInsert",error);
if (!beforeInsertFunction.isError())
{
QJSValue insertListObj = JSEngine.newQObject(&insertObject);
QJSValue result = beforeInsertFunction.call(QJSValueList() << "tmpTable" << insertListObj);
if (result.isError())
{
log("Error calling BeforInsert JS function.");
return 1;
}
else
{
log("JS function seems to be ok");
for (int pos = 0; pos < insertObject.count(); pos++)
{
log(insertObject.itemName(pos) + "-" + insertObject.itemValue(pos));
}
return 1;
}
}
else
{
log("Error evaluating BeforInsert JS function. [" + error + "]");
return 1;
}
I can see that the parameter "table" is passing properly but the rest of the code is not working. I guess I cannot do:
var index = data.getIndexByColumnName("tmpfield");
Any idea what am I doing wrong? and what else should I do to make it work?
Thanks,
In order to access properties or invoke methods of QObjects passed to QJSEngine (or to QML), you need to declare them using Q_PROPERTY and Q_INVOKABLE macros in your QObject-derived class declaration.
Please see the Qt documentation for more details: Exposing Attributes of C++ Types to QML

QNetworkAccessManager not emitting finished signal with this code

so i have been browsing the previous questions before about this issue, but i could not find a solution for my code.
cpp file of dialog
------------------------------------------------
#include "everesult.h"
#include "ui_everesult.h"
everesult::everesult(QWidget *parent) :
QDialog(parent),
ui1(new Ui::everesult)
{
ui1->setupUi(this);
}
everesult::~everesult()
{
delete ui1;
}
void everesult::setmodel(QStandardItemModel *model)
{
ui1->listView->setModel(model);
}
void everesult::on_buttonBox_clicked(QAbstractButton *button)
{
EveReprocess M_;
QModelIndex Selectedindex = ui1->listView->currentIndex();
QModelIndex StationIdsindex = ui1->listView->model()->index(0, 1);
int typeID = 0;
int stationID = 0;
stationID = ui1->listView->model()->data(StationIdsindex, Qt::DisplayRole).toInt();
typeID = ui1->listView->model()->data(Selectedindex, Qt::DisplayRole).toInt();
M_.GetMaterials(typeID, stationID);
}
--------------------------------------------------
Getmaterial and replyFinished from main window.
--------------------------------------------------
void EveReprocess::GetMaterials(int typeId, int stationid)
{
//get typeid from material list
this->modelMaterial = new QSqlQueryModel();
modelMaterial->setQuery(QString("SELECT tm.quantity, tm.materialTypeID, t.typeName FROM invTypeMaterials tm INNER JOIN invTypes t ON t.TypeID = tm.materialTypeId WHERE tm.TypeID=%1 ").arg(typeId));
if (!modelMaterial->query().exec())
qDebug() << modelMaterial->query().lastError();
//Set eve Central Url with typeids
QUrl url = QUrl("http://api.eve-central.com/api/marketstat?");
QUrlQuery q;
int numRows = modelMaterial->rowCount();
for (int row = 0; row < numRows; ++row)
{
QModelIndex index = modelMaterial->index(row, 1);
q.addQueryItem( QString("typeid"), QString::number(modelMaterial->data(index, Qt::DisplayRole).toInt()));
}
q.addQueryItem( QString("usesystem"), QString::number(stationid));
//set created url and connect
url.setQuery(q);
qDebug() << url;
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply *)));
manager->get(QNetworkRequest(url) );
}
void EveReprocess::replyFinished(QNetworkReply *reply)
{
qDebug() << "replyFinished called";
if ( reply->error() != QNetworkReply::NoError ) {
qDebug() << "Request failed, " << reply->errorString();
emit replyFinished(false);
return;
}
qDebug() << "Request succeeded";
//process with xmlreader and get values
processSearchResult( reply);
}
some of the code is here, i think it should be somewhere here, as the rest works fine.
this issue showed up after i use a dialog to let user pick a int from a list.
below is the function that calls the dialog that i have made for this. sorry about code format wil clean it up after it is working
void EveReprocess::Search_TypeId(QString ItemName, QString SystemName)
{
QList<int> TypeIdList;
QList<int> StationIdList;
modelIds = new QStandardItemModel(10,2,this);
if !(db.isOpen()) return;
this->queryItem = new QSqlQuery;
queryItem->prepare("SELECT typeID FROM invTypes WHERE invTypes.typeName LIKE ? AND invTypes.groupID NOT IN (268,269,270)AND published= 1");
ItemName.prepend("%");
ItemName.append("%");
queryItem->bindValue(0, ItemName);
this->queryStation = new QSqlQuery;
queryStation->prepare("SELECT solarSystemID FROM mapSolarSystems WHERE mapSolarSystems.solarSystemName LIKE ?");
SystemName.prepend("%");
SystemName.append("%");
queryStation->bindValue(0, SystemName);
if(!queryStation->exec() || !queryItem->exec() )
{
qDebug() << queryItem->lastError().text();
qDebug() << queryItem->lastQuery();
qDebug() << queryStation->lastError().text();
qDebug() << queryStation->lastQuery();
}
while( queryStation->next())
{
StationIdList.append(queryStation->value(0).toInt());
}
while(queryItem->next())
{
TypeIdList.append(queryItem->value(0).toInt());
}
for (int i = 0; i < StationIdList.count(); ++i)
{
modelIds->setItem(i,1,new QStandardItem(QString::number(StationIdList.at(i))));
}
for (int i = 0; i < TypeIdList.count(); ++i)
{
modelIds->setItem(i,0,new QStandardItem(QString::number(TypeIdList.at(i))));
}
//
everesult Dialog;
Dialog.setmodel(modelIds);
Dialog.exec();
}
Before you proceed any further, some of your code is allows SQL injections. Even when it's not a security hole, it'll still lead to bugs. Instead of using string substitution in SQL queries, you should be using bindings.
Your problem is here:
everesult Dialog;
Dialog.setmodel(modelIds);
Dialog.exec();
The exec() is a blocking function - it blocks the main event loop until the dialog is dismissed. Thus the signals from the threaded network access manager never get delivered to your objects.
You should display the dialog box asynchronously, like so:
everesult * dialog = new everesult;
dialog->setModel(modelIds);
dialog->show();
connect(dialog, SIGNAL(accepted()), dialog, SLOT(deleteLater());
connect(dialog, SIGNAL(rejected()), dialog, SLOT(deleteLater());
Note that it's misleading to have type names starting with lower case and variable names starting with upper case. Qt's convention is the opposite, and it's useful to retain it unless you have a good reason to do otherwise.
DO Parameter Binding in SQL Queries
QSqlQuery query("SELECT .. WHERE tm.TypeID=:typeid");
query.bindValue(":typeid", typeId);
QSqlQueryModel model;
model.setQuery(query);
DON'T DO String Substitution in SQL Queries
setQuery(QString("SELECT ... WHERE tm.TypeID=%1 ").arg(typeId));

Qt: QPSQL prepare doesn't work in class

I tried to outsource the code for the connection to a local PostgreSQL server from my "main.cpp" file into a seperate class called "database.cpp".
The connection worked just fine, when I had the code in the "main.cpp":
main.cpp
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
QSqlQuery query(db);
qint32 declareConnection()
{
db.setHostName("127.0.0.1");
db.setPort(5432);
db.setDatabaseName("postgres");
db.setUserName("postgres");
db.setPassword("password");
return 0;
}
qint32 createUser(QString username, QString password)
{
if (db.open())
{
db.transaction();
query.prepare("INSERT INTO users (name, password) VALUES (?, ?);");
query.bindValue(0, username);
query.bindValue(1, toMD5(password));
query.exec();
query.finish();
db.commit();
db.close();
}
return 0;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Declare Database: " << declareConnection();
qDebug() << "Create User: " << createUser("Testuser", "Testpassword");
return a.exec();
}
But after I put the functions in the "database.cpp", the prepare.query() fails every time I try to execute it.
Here's my current code:
main.cpp
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Database postgre;
qDebug() << "Declare Database: " << postgre.declareConnection();
qDebug() << "Create User: " << postgre.createUser("Testuser", "Testpassword");
return a.exec();
}
Database.h
class Database : public QObject
{
Q_OBJECT
public:
explicit Database(QObject *parent = 0);
qint32 declareConnection();
qint32 createUser(QString username, QString password);
QSqlDatabase db();
};
Database.cpp
QSqlDatabase Database::db()
{
return QSqlDatabase::database();
}
qint32 Database::declareConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("127.0.0.1");
db.setPort(5432);
db.setDatabaseName("postgres");
db.setUserName("postgres");
db.setPassword("password");
return 0;
}
qint32 Database::createUser(QString username, QString password)
{
if (db().open())
{
db().transaction();
QSqlQuery query(db());
query.prepare("INSERT INTO users (name, password) VALUES (?, ?);");
query.bindValue(0, username);
query.bindValue(1, toMD5(password));
query.exec();
query.finish();
db().commit();
db().close();
}
return 0;
}
The declaration of the database seems to work just fine, but when it comes to the "createUser()" function, there is always that problem with the "query.prepare()" command.
The database is open, so that works.
Also the transaction is in an open state, when I check in debug.
The prepare is false.
The query.exec() says: Syntaxerror at end of line LINE 1: EXECUTE ^.
QPSQL: Unable to create query.
WARNING: No open transaction. (But was open???)
The problem is here
void Database::declareConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
you declared local variable db on a stack.
The decision is:
class Database : public QObject
{
...
QSqlDatabase & db() { return m_db; }
private:
QSqlDatabase m_db;
};
void Database::declareConnection()
{
m_db = QSqlDatabase::addDatabase("QPSQL");
...
}

Resources