Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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 @ 1:50 PST

This is my error: ( Line error numbers do not match below code.)

  1. ..\DBWidgetMap\trackersql.cpp: In member function 'QSqlQueryModel* TrackerSql::getListModel()':
  2. ..\DBWidgetMap\trackersql.cpp:38: error: cannot convert 'QSqlQueryModel' to 'QSqlQueryModel*' in return

Here are what the classes do:

MainWindow : Main control class.

TrackerSQL : Sql queries.

List: Widget manages visual representation of ListUI.

ListUI: Visual representation for 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_OBJECT

public:
    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...
} 

Code: 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;
}

Code: 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();
}

Also can I propose a moderator add the tag Qt?

share|improve this question

1 Answer

up vote 0 down vote accepted

Advice from StackOverflow along with the below change to above. Other changes from advice are already reflected in code.

QSqlQueryModel *TrackerSql::getListModel(void){
    return &dbQueryCard;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.