Aaron Aguayo Martinez Aaron Aguayo Martinez - 1 year ago 129
SQL Question

Passing ACTUAL variable to a new class using QT (C++) + SQL

I'm new to QT/C++ and I'm probably missing something essential and I can't make it to work.

I explain a bit.

I have 2 classes (MainWindow.cpp/.h and SimulationTask.cpp/.h).

MainWindow got a Menu that's using some variables to load via SQL the info.
Once user has clicked the last item on the list, it loads from SQL database a string with the name of our new variable, called tablename.

Now we have a second window, called SimulationTask that needs the variable tablename, but every time I use it, it uses it with its default value defined in mainwindow.h, instead of the actual value, as we did get in our mainwindow.cpp.

The code is long already, I will show you the essential parts so you can contrast.

//Get table method(at MainWindow.cpp)

QString MainWindow::getTableName()

QSqlQuery query;
query.prepare("SELECT candata_table,bitrate "
"FROM system_list, maker_data "
"WHERE system_list.specific_id=maker_data.specific_id "
"AND system_list.specific_id="
"(SELECT maker_data.specific_id FROM maker_data WHERE maker_data.manufacturer='"+varmnf+"' AND maker_data.model='"+varmodel+"' AND maker_data.year='"+varyear+"')");
tablename = query.value(0).toString();

qDebug() << (query.lastQuery());

return tablename;

If I call it on my MainWindow.cpp class, in a pushbutton for example, it does load values in Sql query (varmnf , varmodel, varyear) perfectly and it returns tablename without problem, but if I call it from my simulationTask.cpp, it get initializacion values for varmnf , varmodel, varyear, so obviously, the query doesnt throw any result, and tablename either.

#include "mainwindow.h"
//TABLE NAME (at simulationstask.cpp)
MainWindow mainwindow;

mainwindow.getTableName(); // --> This is returning tablename = ''
qDebug() << ("Hello");
qDebug() << (tablename);

Console Output
Console Log image


This is my MainWindow.h:


#include <QMainWindow>
#include "simulationtask.h"

#define debugprefix << __LINE__ << " "
namespace Ui {
class MainWindow;

class MainWindow : public QMainWindow


QString getTableName();

QSqlDatabase mydb;

QString varmnf;
QString varmodel;
QString varyear;
QString tablename;
QString tablenameUpdated;
QString bitrate;
QString varsystem;

This is part of my MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "simulationtask.h"

void MainWindow::on_ModelList_clicked(const QModelIndex &index)

QString var2=ui->ModelList->model()->data(index).toString();
QSqlQueryModel * modal=new QSqlQueryModel();
QSqlQuery* query=new QSqlQuery(mydb);

query->prepare("SELECT year FROM maker_data WHERE manufacturer='"+varmnf+"' AND model='"+var2+"'");

qDebug() <<(query->lastQuery());

void MainWindow::on_mnfList_clicked(const QModelIndex &index){codehere}
void MainWindow::on_YearList_clicked(const QModelIndex &index){codehere}

Basically what It do is update on click the index and send to my variables (mnfvar [manufacturer] -> modelvar[model] -> yearvar [fabrication year] -> systemvar [type os system] ) With those variables with the correct values from SQL, I can call the function above (that's another query) to get tablename. The problem comes when I call it from simulationTask.cpp, and it doesnt have those variables, I tried returning tablename in function but it's not working.

Answer Source

It seems varmnf , varmodel, varyear variables are a part of MainWindow class and you create one instance, initialize these variables with your non-default values and it works. Then you create a new instance within SimulationTask.cpp

MainWindow mainwindow; //< new instance within SimulationTask.cpp

...this object has unintialized varmnf , varmodel, varyear variables. This causes the problem.

Any solution?

You can use getters & setters to set those variables outside MainWindow class

    class MainWindow {
      QString varmnf, varmodel, varyear;
      void setVarmnf(const QString &);
      void setVarmodel(const QString &);
      void setVaryear(const QString &);

    void setVarmnf(const QString &mnf) { varmnf = mnf; }
    void setVarmodel(const QString &model) { varmodel = model; }
    void setVaryear(const QString &year) { varyear = year; }

    MainWindow mainwindow;
    mainwindow.setVarmnf("FIAT"); // or any other value, from a function or sth
    mainwindow.setVaryear("01/2005 - 10/2009");
    QString tablename = mainwindow.getTableName();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download