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. Error passing an object.
QtWS25 Last Chance

Error passing an object.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.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.
  • I Offline
    I Offline
    iyustlop
    wrote on last edited by p3c0
    #1

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

    class initDB
    {
    public:
        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(?, ?, ?)");
    
        query.addBindValue(p.GetNumber());
        query.addBindValue(p.Getnombre());
        query.addBindValue(p.Getapellidos());
      
        query.exec();
        return QSqlError();
    }
    

    The output gives me this error:

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

    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

    A 1 Reply Last reply
    0
    • I iyustlop

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

      class initDB
      {
      public:
          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(?, ?, ?)");
      
          query.addBindValue(p.GetNumber());
          query.addBindValue(p.Getnombre());
          query.addBindValue(p.Getapellidos());
        
          query.exec();
          return QSqlError();
      }
      

      The output gives me this error:

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

      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

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      1
      • I Offline
        I Offline
        iyustlop
        wrote on last edited by p3c0
        #3
        class Person
        {
        public:
            QString GetNumber();
            void SetNumber (QString number);
            QString GetName();
            void SetName (QString name);
            QString GetSurname();
            void SetSurname (QString surname);
            Person();
        private:
            QString number;
            QString name;
            QString surname;
        };
        
        A 1 Reply Last reply
        0
        • I iyustlop
          class Person
          {
          public:
              QString GetNumber();
              void SetNumber (QString number);
              QString GetName();
              void SetName (QString name);
              QString GetSurname();
              void SetSurname (QString surname);
              Person();
          private:
              QString number;
              QString name;
              QString surname;
          };
          
          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          1
          • I Offline
            I Offline
            iyustlop
            wrote on last edited by
            #5

            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&'

            1 Reply Last reply
            0
            • m.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                query.addBindValue(p.GetNumber());
                ->
                QString Numb("0000000000");
                query.addBindValue(Numb);

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

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

                  query.addBindValue(QVariant(p.GetNumber()));

                  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.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  1
                  • I Offline
                    I Offline
                    iyustlop
                    wrote on last edited by
                    #9

                    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: http://doc.qt.io/qt-5/qtsql-books-example.html

                    A 1 Reply Last reply
                    0
                    • I iyustlop

                      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: http://doc.qt.io/qt-5/qtsql-books-example.html

                      A Offline
                      A Offline
                      ambershark
                      wrote on last edited by
                      #10

                      @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:

                      initdb.h:

                      #ifndef INITDB_H
                      #define INITDB_H
                      
                      #include <QVariant>
                      #include <QSqlError>
                      
                      class QString;
                      class QSqlQuery;
                      class QDate;
                      
                      class InitDB
                      {
                      public:
                              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);
                      };
                      
                      #endif
                      

                      initdb.cpp:

                      #include "initdb.h"
                      #include <QSqlDatabase>
                      #include <QString>
                      #include <QSqlQuery>
                      #include <QDate>
                      
                      void InitDB::addBook(QSqlQuery &q, const QString &title, int year, const QVariant &autho
                      rId,
                                   const QVariant &genreId, int rating)
                      {
                          q.addBindValue(title);
                          q.addBindValue(year);
                          q.addBindValue(authorId);
                          q.addBindValue(genreId);
                          q.addBindValue(rating);
                          q.exec();
                      }
                      
                      QVariant InitDB::addGenre(QSqlQuery &q, const QString &name)
                      {
                          q.addBindValue(name);
                          q.exec();
                          return q.lastInsertId();
                      }
                      
                      QVariant InitDB::addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
                      {
                          q.addBindValue(name);
                          q.addBindValue(birthdate);
                          q.exec();
                          return q.lastInsertId();
                      }
                      
                      QSqlError InitDB::initDb()
                      {
                          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                          db.setDatabaseName(":memory:");
                      
                          if (!db.open())
                              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,
                       28));
                      
                          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();
                      }
                      

                      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                      1 Reply Last reply
                      2
                      • I Offline
                        I Offline
                        iyustlop
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        1

                        • Login

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