[Solved]Extending QSqlQueryModel
-
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; }
}@