Results 1 to 8 of 8

Thread: Passing ACTUAL variable to a new class, getting wrong value

  1. #1
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5

    Default Passing ACTUAL variable to a new class, getting wrong value

    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.

    Qt Code:
    1. //Get table method(at MainWindow.cpp)
    2.  
    3. QString MainWindow::getTableName()
    4.  
    5. connOpen();
    6. QSqlQuery query;
    7. query.prepare("SELECT candata_table,bitrate "
    8. "FROM system_list, maker_data "
    9. "WHERE system_list.specific_id=maker_data.specific_id "
    10. "AND system_list.specific_id="
    11. "(SELECT maker_data.specific_id FROM maker_data WHERE maker_data.manufacturer='"+varmnf+"' AND maker_data.model='"+varmodel+"' AND maker_data.year='"+varyear+"')");
    12. query.exec();
    13. query.first();
    14. connClose();
    15. tablename = query.value(0).toString();
    16.  
    17. qDebug() << (query.lastQuery());
    18.  
    19. return tablename;
    To copy to clipboard, switch view to plain text mode 


    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.


    Qt Code:
    1. #include "mainwindow.h"
    2. //TABLE NAME (at simulationstask.cpp)
    3. MainWindow mainwindow;
    4.  
    5. mainwindow.getTableName(); // --> This is returning tablename = ''
    6. qDebug() << ("Hello");
    7. qDebug() << (tablename);
    To copy to clipboard, switch view to plain text mode 

    **Console Output**
    [Console Log image][1]


    [1]: http://i.stack.imgur.com/ZADL6.jpg


    This is my MainWindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "simulationtask.h"
    6.  
    7. #define debugprefix << __LINE__ << " "
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16.  
    17. public:
    18.  
    19. QString getTableName();
    20.  
    21. //FUNCTIONS VARIABLES
    22.  
    23. QString varmnf;
    24. QString varmodel;
    25. QString varyear;
    26. QString tablename;
    27. QString tablenameUpdated;
    28. QString bitrate;
    29. QString varsystem;
    To copy to clipboard, switch view to plain text mode 

    This is part of my MainWindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. #include "simulationtask.h"
    5.  
    6. void MainWindow::on_ModelList_clicked(const QModelIndex &index)
    7. {
    8. //cleaning
    9. ui->System_list->setModel(emptymodal);
    10. varsystem="";
    11. tablename="";
    12. bitrate="";
    13.  
    14. QString var2=ui->ModelList->model()->data(index).toString();
    15. varmodel=ui->ModelList->model()->data(index).toString();
    16. connOpen();
    17. QSqlQuery* query=new QSqlQuery(mydb);
    18.  
    19. query->prepare("SELECT year FROM maker_data WHERE manufacturer='"+varmnf+"' AND model='"+var2+"'");
    20. query->exec();
    21. modal->setQuery(*query);
    22. ui->YearList->setModel(modal);
    23. connClose();
    24.  
    25. qDebug() <<(query->lastQuery());
    26.  
    27. }
    28. void MainWindow::on_mnfList_clicked(const QModelIndex &index){codehere}
    29. void MainWindow::on_YearList_clicked(const QModelIndex &index){codehere}
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    Your code in simulationTask.cpp calls getTableName() on a new, local, MainWindow object.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    Hello anda_skoa!

    So I guess there must be a way to call it on the same "instance"?

    How could I do that?

    Thanks!

    EDIT:

    Here are images of my program and console log:
    app1.jpg
    app2.jpg
    app3.jpg
    Last edited by trainer92; 21st August 2016 at 21:25.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    Quote Originally Posted by trainer92 View Post
    So I guess there must be a way to call it on the same "instance"?

    How could I do that?
    By calling the method on that instance.

    Since your code doesn't show how the relation between SimulationTask and MainWindow is (or I missed it) it is hard to make any suggestions on getting access to that instance.
    Maybe you don't even need to do that at all.

    E.g. SimulationTask instance might be created in MainWindow and the they could just get the relevant values right then.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    You should also use QSqlQuery::bindValue instead of concatenating variables in-line with the SQL as your code may be vulnerable to SQL injection attacks.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #6
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    Quote Originally Posted by anda_skoa View Post
    By calling the method on that instance.

    Since your code doesn't show how the relation between SimulationTask and MainWindow is (or I missed it) it is hard to make any suggestions on getting access to that instance.
    Maybe you don't even need to do that at all.

    E.g. SimulationTask instance might be created in MainWindow and the they could just get the relevant values right then.

    Cheers,
    _
    Hello, I attach a diagram explaining how it works.

    diagram.jpg

    In general fact, MainWindow is holding all SQL thing (Manufacturers, and model, year, system.. all loaded via SQL and outputed to QViewList in program). Once user navigate to last category, I got a query getting the Tablename needed, that's also at SQL tables, so I get it and store on a variable called "tablename".

    Now I have class MainWindow, with loaded menu, and tablename variable

    I need class SimulationTask, to get this variable, to perform its work, but I can't find a way to pass it.

    (SimulationTask is called with a button with the following code):

    simulationTask *simulationtask;
    simulationtask =new simulationTask(this);
    simulationtask->show();
    Then a new UI opens, and I need the values from MainWindow, but I'm spending like 8hours with that stupid thing and I cant get it <_>.

    Thanks for your help!

    Quote Originally Posted by jefftee View Post
    You should also use QSqlQuery::bindValue instead of concatenating variables in-line with the SQL as your code may be vulnerable to SQL injection attacks.
    I will get a view on this, thanks!

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    You could pass a MainWindow* to your simulationtask constructor, which you'd then save as a simulationtask member variable. Then add getter/setter methods to MainWindow to get/set the variables you need, i.e.:
    Qt Code:
    1. QString MainWindow::getFoo()
    2. {
    3. return m_foo;
    4. }
    5.  
    6. void MainWindow::setFoo(const QString& foo)
    7. {
    8. m_foo = foo;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Edit: In fact, your post above shows you passing "this" to the simulationtask constructor. Is "this" the MainWindow instance? If so, just save that value in a simulationtask member variable and then you can use that to get the values once you add getter methods like so: m_mainwindow->getFoo(), etc.
    Last edited by jefftee; 21st August 2016 at 23:26.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #8
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5

    Default Re: Passing ACTUAL variable to a new class, getting wrong value

    Quote Originally Posted by jefftee View Post
    You could pass a MainWindow* to your simulationtask constructor, which you'd then save as a simulationtask member variable. Then add getter/setter methods to MainWindow to get/set the variables you need, i.e.:
    Qt Code:
    1. QString MainWindow::getFoo()
    2. {
    3. return m_foo;
    4. }
    5.  
    6. void MainWindow::setFoo(const QString& foo)
    7. {
    8. m_foo = foo;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Edit: In fact, your post above shows you passing "this" to the simulationtask constructor. Is "this" the MainWindow instance? If so, just save that value in a simulationtask member variable and then you can use that to get the values once you add getter methods like so: m_mainwindow->getFoo(), etc.
    Hello!! Thanks a lot for suggestions, I openned eyes with last post from anda_skoa

    Quote Originally Posted by anda_skoa View Post
    By calling the method on that instance.

    Since your code doesn't show how the relation between SimulationTask and MainWindow is (or I missed it) it is hard to make any suggestions on getting access to that instance.
    Maybe you don't even need to do that at all.

    E.g. SimulationTask instance might be created in MainWindow and the they could just get the relevant values right then.

    Cheers,
    _
    I though on.. "Why not do this inversed?".. and this is what i done, I just did set the variable of simulationtask from my mainwindow class as follow:

    In mainwindow.cpp
    simulationTask *simulationtask;
    simulationtask =new simulationTask(this);

    simulationtask->finalTablename = setTableName(); <--- This return me the famous "tablename" value x)

    simulationtask->show();
    I have been here for over 8 hours for this.. I feel stupid, but this is how it works..

    Thanks everyone for your help, it's really appreaciated! ^^

Similar Threads

  1. Passing variable to stylesheet in runtime
    By ndev in forum Qt Programming
    Replies: 5
    Last Post: 23rd February 2015, 12:58
  2. Passing variable from tab to tab in QTabWidget
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2014, 08:04
  3. connect() variable passing issue
    By hojoff79 in forum Newbie
    Replies: 6
    Last Post: 1st February 2011, 11:18
  4. Replies: 3
    Last Post: 8th June 2007, 20:13
  5. passing the wrong signal
    By veilig in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2007, 22:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.