Results 1 to 7 of 7

Thread: Pass QSqlQuery result to object

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Pass QSqlQuery result to object

    Hi there,

    I am trying to pass on a QSqlQuery result, which is one line from a SQLite database, to an object. Is that possible and if so how should the syntax be?
    I tried to use QSqlQuery as a data type but I guess that is not correct:
    Qt Code:
    1. .../mainwindow.cpp:70: Error: no matching constructor for initialization of 'Employee'
    2. team[sIndex] = Employee(qry, level);
    3. ^ ~~~~~~~~~~
    4. .../employee.h:9: candidate constructor not viable: no known conversion from 'QSqlQuery *' to 'const QSqlQuery' for 1st argument; dereference the argument with *
    5. Employee(const QSqlQuery &query, const QString &level);
    6. ^
    To copy to clipboard, switch view to plain text mode 
    If this would be the right way, what would be the correct data type?

    My goal is to create a Qvector<Employee> called team that holds all the team members. The employee objects within the vector shall hold a bunch of information from a SQLite database. After selecting the right employee in my GUI the correct data (one row from the db) should be loaded and a new employee object should be created. Within the class Employee the basic data should be reworked. I am not interested in the basic data from the db but the parameters that could be calculated from the basic data. So I would like to pass on the query result to the object and have the class do all the conversion.
    Is this a clever way to do that or am I barking up the wrong tree?
    The code works as long as I use two QStrings, with the exception that I cannot declare the vector Qvector<Employee> team without the two arguments defined in the class. So far I was not able to find a work around that issue. How can I declare an empty vector from the class Employee without passing on any values?

    Thanks for your input in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Pass QSqlQuery result to object

    The error you get is because the pointer-to-QSqlQuery you pass the constructor is not compatible with a reference-to-const-QSqlQuery the constructor is expecting.

    You could:
    • adjust the expectations of the constructor to expect a pointer,
    • change how you call the constructor to use an actual instance (i.e. *qry),
    • look at passing a QSqlRecord obtained from QSqlQuery::record() by const-reference, or
    • extract the necessary fields and pass them individually to separate parameters of the constructor.

  3. The following user says thank you to ChrisW67 for this useful post:

    KeineAhnung (23rd April 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Pass QSqlQuery result to object

    Hi Chris,

    thanks for the quick help. I guess I should learn more about pointers and instances. I went with the instance suggestion and now I get past this but it is still not compiling. As I thought, the issue is at declaring the vector:
    Qt Code:
    1. Undefined symbols for architecture x86_64:
    2. "Employee::Employee()", referenced from:
    3. QVector<Employee>::defaultConstruct(Employee*, Employee*) in mainwindow.o
    4. ld: symbol(s) not found for architecture x86_64
    5. clang: error: linker command failed with exit code 1 (use -v to see invocation)
    6. make: *** [Team.app/Contents/MacOS/Team] Error 1
    To copy to clipboard, switch view to plain text mode 
    So how can I declare a QVector with a custom class that needs an input that I do not have at that moment?
    Here is how my class looks:
    Qt Code:
    1. class Employee{
    2. public:
    3. Employee();
    4. Employee(const QSqlQuery *query, const QString &level);
    5. [...]
    6. }
    To copy to clipboard, switch view to plain text mode 
    I thought by using Employee(); I can create an object of Employee without the need to pass on anything. Is this not correct?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Pass QSqlQuery result to object

    Your linker error implies you are doing something odd with your QVector but we cannot see it.

    This is what a working example looks like:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QVector>
    3. #include <QDebug>
    4.  
    5. // Typically in employee.h
    6. class Employee {
    7. public:
    8. Employee();
    9. Employee(const QString &name, const QString &job);
    10.  
    11. QString name() const;
    12. QString job() const;
    13. // ...
    14.  
    15. private:
    16. QString m_name;
    17. QString m_job;
    18. };
    19.  
    20. // Typically in employee.cpp
    21. Employee::Employee() { }
    22. Employee::Employee(const QString &name, const QString &job): m_name(name), m_job(job) { }
    23. QString Employee::name() const { return m_name; }
    24. QString Employee::job() const { return m_job; }
    25.  
    26. // Typically in main.cpp
    27. int main(int argc, char *argv[])
    28. {
    29. QCoreApplication app(argc, argv);
    30.  
    31. QVector<Employee> employees; // empty by default
    32.  
    33. employees << Employee();
    34. employees.append(Employee("Tom", "Burger flipper"));
    35.  
    36. // This is similar to your initial example
    37. // The vector does not already have index 2 so this will fail badly at run time. See QVector::operator[].
    38. // employees[2] = Employee("Dick", "Burger wrapper");
    39.  
    40. qDebug() << employees.count();
    41. foreach(const Employee &emp, employees) {
    42. qDebug() << emp.name() << emp.job();
    43. }
    44.  
    45. return 0;
    46. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 23rd April 2014 at 07:48.

  6. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Pass QSqlQuery result to object

    Okay, somehow I got it to work by adding {}
    Qt Code:
    1. class Employee{
    2. public:
    3. Employee(){};
    4. Employee(const QSqlQuery *query, const QString &level);
    5. [...]
    6. }
    To copy to clipboard, switch view to plain text mode 
    Can anyone tell me why this did the trick?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Pass QSqlQuery result to object

    By adding {} you have defined the implementation of the default constructor that was, presumably, missing before.

  8. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Pass QSqlQuery result to object

    Thanks for the help! In this five minutes today I got further with my program than in the entire day yesterday...

Similar Threads

  1. Replies: 3
    Last Post: 10th February 2014, 18:46
  2. Replies: 0
    Last Post: 28th May 2012, 19:56
  3. Replies: 0
    Last Post: 9th August 2011, 10:15
  4. QSqlQuery return result
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 07:55
  5. ActiveQt: How to pass image to ActiveX object?
    By AndreasSchlempp in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2009, 12:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.