Important: Please read the Qt Code of Conduct -

Error passing an object.

  • I have created a function initdb.h with the following methods:

    class initDB
        QSqlError startDB();
        QSqlError insertDB(Person persona);

    and also I have created initdb.cpp with the following insert method:

    QSqlError insertDB(Person p)
        QSqlQuery query;
        query.prepare("insert into person values(?, ?, ?)");
        return QSqlError();

    The output gives me this error:

    ..\initdb.cpp:42:51: error: no matching function for call to 'QSqlQuery::addBindValue(QString)'

    I work with Java and I am not an expert developer. Could anyone help me? with p.GetNumber is getting an error.

    Thank you very much in advance.

    Edit - Code tags - p3c0

  • Moderators

    @iyustlop Can you show us the header for the Person class? My guess is it isn't returning a value that can be put into a QVariant.

    Here is the definition of addBindValue:

    addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In)

    So the compiler is saying p.GetNumber() is returning something that doesn't fit into that const QVariant &val parameter.

  • class Person
        QString GetNumber();
        void SetNumber (QString number);
        QString GetName();
        void SetName (QString name);
        QString GetSurname();
        void SetSurname (QString surname);
        QString number;
        QString name;
        QString surname;

  • Moderators

    @iyustlop That's weird. The QVariant should handle QString just fine. That error is weird.

    I will need to throw that scenario into a compiler to see if I can duplicate it. I'm heading to bed right now but will check it out in the morning.

    In the meantime can you post the full build log? Just in case there is something other than that error that would explain it.

  • The log gives me the following information.

    C:\Qt\5.7\mingw53_32\include\QtSql/qsqlquery.h:107:10: note: no known conversion for argument 1 from 'QString' to 'const QVariant&'

  • Hi,
    maybe you just have to include the <QVariant> header file.

  • Lifetime Qt Champion

    Also I would try directly with QString to see if error still comes.

    QString Numb("0000000000");

  • Moderators

    Another thing to try is to force a QVariant by doing something like:


    With your new error message it's definitely having trouble with the QString->QVariant conversion. So by putting the string directly into a QVariant you should be ok.

  • Thank very much for your effort, indeed.

    I included QVariant and it was a mess. A lot errors appears.

    I have decided rebuild everything from scratch focus on this problem.

    I am following the example of books, the problem is that in this example the implementation of the database is in the header file and I am trying to write all the code in the source file.

    A suggestion: Is it possible update this example not using the header file for all the connection to the data base??

    link to the example:

  • Moderators

    @iyustlop Sure here ya go I'll make it into header/cpp files for you. You could even make it into a class to be more C++ like which is what I did:


    #ifndef INITDB_H
    #define INITDB_H
    #include <QVariant>
    #include <QSqlError>
    class QString;
    class QSqlQuery;
    class QDate;
    class InitDB
            QSqlError initDb();
            void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
                                     const QVariant &genreId, int rating);
            QVariant addGenre(QSqlQuery &q, const QString &name);
            QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate);


    #include "initdb.h"
    #include <QSqlDatabase>
    #include <QString>
    #include <QSqlQuery>
    #include <QDate>
    void InitDB::addBook(QSqlQuery &q, const QString &title, int year, const QVariant &autho
                 const QVariant &genreId, int rating)
    QVariant InitDB::addGenre(QSqlQuery &q, const QString &name)
        return q.lastInsertId();
    QVariant InitDB::addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
        return q.lastInsertId();
    QSqlError InitDB::initDb()
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        if (!
            return db.lastError();
        QStringList tables = db.tables();
        if (tables.contains("books", Qt::CaseInsensitive)
            && tables.contains("authors", Qt::CaseInsensitive))
            return QSqlError();
        QSqlQuery q;
        if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar,
     author integer, genre integer, year integer, rating integer)")))
            return q.lastError();
        if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar
    , birthdate date)")))
            return q.lastError();
        if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)
            return q.lastError();
        if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
            return q.lastError();
        QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
        QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2))
        QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4,
        if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
            return q.lastError();
        QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
        QVariant fiction = addGenre(q, QLatin1String("Fiction"));
        QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
        if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating)
    values(?, ?, ?, ?, ?)")))
            return q.lastError();
        addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
        addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
        addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
        addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
        addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
        addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
        addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
        addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
        addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
        return QSqlError();

  • I have started from scratch following your indications.

    Now I have a method to insert and I am finishing a method to get from database (the cause of the delay).

    As I mentioned before, thank you very much indeed for your effort.

    I put to solved this post.

Log in to reply