trainer92
21st August 2016, 20:54
Hello everyone, 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()
connOpen();
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+"')");
query.exec();
query.first();
connClose();
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][1]
[1]: http://i.stack.imgur.com/ZADL6.jpg
This is my MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "simulationtask.h"
#define debugprefix << __LINE__ << " "
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QString getTableName();
//FUNCTIONS VARIABLES
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)
{
//cleaning
ui->System_list->setModel(emptymodal);
varsystem="";
tablename="";
bitrate="";
QString var2=ui->ModelList->model()->data(index).toString();
varmodel=ui->ModelList->model()->data(index).toString();
connOpen();
QSqlQueryModel * modal=new QSqlQueryModel();
QSqlQuery* query=new QSqlQuery(mydb);
query->prepare("SELECT year FROM maker_data WHERE manufacturer='"+varmnf+"' AND model='"+var2+"'");
query->exec();
modal->setQuery(*query);
ui->YearList->setModel(modal);
connClose();
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.
Also tried with get and set methods and not working, I tried a dirty way too, I make a LineTextEdit in 1st window UI, where I sent my tablename record, well, when loading from 2nd window UI it also loads the INITIALIZED value "test" (and not the actual one being showed "gpunto")... I think it's something bad with headers or ui initialization because it's not normal.
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()
connOpen();
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+"')");
query.exec();
query.first();
connClose();
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][1]
[1]: http://i.stack.imgur.com/ZADL6.jpg
This is my MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "simulationtask.h"
#define debugprefix << __LINE__ << " "
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QString getTableName();
//FUNCTIONS VARIABLES
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)
{
//cleaning
ui->System_list->setModel(emptymodal);
varsystem="";
tablename="";
bitrate="";
QString var2=ui->ModelList->model()->data(index).toString();
varmodel=ui->ModelList->model()->data(index).toString();
connOpen();
QSqlQueryModel * modal=new QSqlQueryModel();
QSqlQuery* query=new QSqlQuery(mydb);
query->prepare("SELECT year FROM maker_data WHERE manufacturer='"+varmnf+"' AND model='"+var2+"'");
query->exec();
modal->setQuery(*query);
ui->YearList->setModel(modal);
connClose();
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.
Also tried with get and set methods and not working, I tried a dirty way too, I make a LineTextEdit in 1st window UI, where I sent my tablename record, well, when loading from 2nd window UI it also loads the INITIALIZED value "test" (and not the actual one being showed "gpunto")... I think it's something bad with headers or ui initialization because it's not normal.