[Solved]Passing QSqlQueryModel
-
Can someone help me figure out what I am doing wrong?
I am trying to pass QSqlQueryModel reference to my List widget so it can add list items. This code below should clear db contents of my table for now as a test because I plan on building QWidgetMapper into List later.
Code & Edit! 8-15 at 1:50 PST
This is my error: ( Line error numbers do not match below code.)
- ..\DBWidgetMap\trackersql.cpp: In member function 'QSqlQueryModel* TrackerSql::getListModel()':
- ..\DBWidgetMap\trackersql.cpp:38: error: cannot convert 'QSqlQueryModel' to 'QSqlQueryModel*' in return
Here is a written breakdown of what I am doing:
MainWindow : Main control class.
TrackerSQL : Sql queries.
List: Widget manages visual representation of ListUI.
ListUI: Visual representation of List. ( Not shown below, no need)MainWindow - > (TrackerSql, List = ( ListUI ))
Code:
MainWindow
@#include <QtGui/QMainWindow>
#include <QLayout>
#include "list.h"
#include "trackersql.h"class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);
~MainWindow();void windowFields(void); // Set the `MainWindow` widget's to be displayed. void createWindowFields(void); // Create widget fields to be displayed. void arrangeWindowFields(void); // Arrange the `MainWindow` layout. List *cardList; // Visual widget for display. Holds a list of cards in database. TrackerSql *sqlModel; // Access to database queries. QHBoxLayout *hCard; // Layout container for `MainWindow`.
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QWidget *mainW = new QWidget;
setCentralWidget(mainW);windowFields(); mainW->setLayout(hCard);
}
MainWindow::~MainWindow(){}
void MainWindow::windowFields(void){
// it works
}void MainWindow::createWindowFields(void){
sqlModel = new TrackerSql;
cardList = new List(sqlModel->getListModel(), this);
}void MainWindow::arrangeWindowFields(void){
// it works...
} @TrackerSQL
@#include <QWidget>
#include <QtSql>class TrackerSql : public QWidget
{
Q_OBJECT
public:
explicit TrackerSql(QWidget *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 createTableQuerys(void); // Create `dbQuery`(s). QSqlQueryModel *getListModel(void);// Pass the refference location to query results QSqlDatabase db; QSqlQueryModel dbQueryCard; QString // connection variables...
};
TrackerSql::TrackerSql(QWidget *parent) :
QWidget(parent)
{
db = QSqlDatabase::addDatabase("QMYSQL");
connectSQL();
}void TrackerSql::connectSQL(void){
setConnection();
createConnection();
createTableQuerys();
}void TrackerSql::setConnection(void){
// it works...
}void TrackerSql::createConnection(void){
// it works...
db.open();
}void TrackerSql::createTableQuerys(void){
dbQueryCard.setQuery("SELECT * FROM Card");
}QSqlQueryModel *TrackerSql::getListModel(void){
return &dbQueryCard;
}@List
@#include "listui.h"
#include <QSqlQueryModel>
#include <QWidgetMapper>class List : public ListUI {
Q_OBJECT
public:
explicit List(QSqlQueryModel *model, QWidget *parent); // Initialize with model reference location of model.
void setListItems(QSqlQueryModel *model); // Set card list. (currently in debug output mode for test)
};List::List(QSqlQueryModel *model, QWidget *parent) : ListUI(parent){
setListItems(model);
}void List::setListItems(QSqlQueryModel *model){
model->clear();
}@ -
in MainWindow::createWindowFields replace
@cardList = new List(sqlModel->getListModel());@
with
@cardList = new List(sqlModel->getListModel(), this);@
if you want the ListUI to be child of MainWindow.Further, there will be another error in List::List. You pass a pointer to a pointer to setListItems while it actually just wants a pointer. (Note that model already is a QSqlQueryModel-Pointer). So just pass model instead of &model.
And finally in List::setListItems you have the pointer model and try to access fields via the dot-operator instead of dereference-dot-operator.