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. [Solved]Extending QSqlQueryModel

[Solved]Extending QSqlQueryModel

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.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.
  • W Offline
    W Offline
    weblife
    wrote on 17 Aug 2012, 06:06 last edited by
    #1

    What exactly am I doing wrong here? I should see the table contents dump out into the console. I can connect and read queries and communicates with other widgets who need it when I extend to QWidget and declare the model as QSqlQueryModel *value; but when I attempt to extend to QSqlQueryModel, nothing. I know the database is open and I am pretty sure I just need to lead it to the QSqlyQueryModel and my attempt is not working.

    Sorry to ask but lastError().text() is returning nothing.

    Here is the class:
    @#include <QObject>
    #include <QSqlQueryModel>
    #include <QDebug>
    #include <QSqlError>

    class TrackerSql : public QSqlQueryModel
    {
    Q_OBJECT
    public:
    TrackerSql(QObject *parent = 0);

    void connectSQL(void);        // Start sql connections and initialize `dbQuery`(s).
    void setConnection(void);     // Set the connection parameters.
    void createConnection(void);  // Start the `db` connection.
    
    void setTableQuery(void);// Create `dbQuery`(s).
    
    QSqlDatabase    db;
    QString         dbHost,
                    dbName,
                    dbUser,
                    dbPass,
                    dbString;
    qint32          dbPort;
    

    };

    TrackerSql::TrackerSql(QObject *parent): QSqlQueryModel(parent) {
    db = QSqlDatabase::addDatabase("QMYSQL");
    connectSQL();
    }

    void TrackerSql::connectSQL(void){
    setConnection();
    createConnection();
    }

    void TrackerSql::setConnection(void){
    dbHost = "";
    dbName = "";
    dbUser = "";
    dbPass = "";
    dbPort = ;
    }

    void TrackerSql::createConnection(void){
    db.setHostName(dbHost);
    db.setPort(dbPort);
    db.setDatabaseName(dbName);
    db.setUserName(dbUser);
    db.setPassword(dbPass);
    db.open();

    qDebug() << db.lastError().text();
    

    }

    void TrackerSql::setTableQuery(void){
    setQuery("SELECT * FROM Card", db);
    qDebug() << lastError().text();

    for (int i = 0; i < rowCount(); ++i) {
        QString firstName = data(index(i,2)).toString();
        QString middleInitial = data(index(i,3)).toString();
        QString lastName = data(index(i,4)).toString();
        QString wholeName = firstName + " " + middleInitial + " " + lastName;
        qDebug() << wholeName;
    }
    

    }@

    Brandon Clark
    www.themindspot.com

    1 Reply Last reply
    0
    • W Offline
      W Offline
      weblife
      wrote on 17 Aug 2012, 13:57 last edited by
      #2

      I am and idiot. Forgot to add my new member call.

      @setTableQuery();@

      All is good, just needed sleep again to take a good look.

      Brandon Clark
      www.themindspot.com

      1 Reply Last reply
      0

      1/2

      17 Aug 2012, 06:06

      • Login

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