Results 1 to 7 of 7

Thread: Qt GUI print global variables

  1. #1
    Join Date
    Apr 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Qt GUI print global variables

    Hi!

    I'm very new at Qt and c++ programming, and I created a Qt GUI and have some global variables that I change when my callbacks are called.
    I would like to print those global variables in my terminal, so I wrote in my main.cpp the following:

    Qt Code:
    1. #include "ros/ros.h"
    2.  
    3. #include "mainwindow.h"
    4. #include <QApplication>
    5. #include "globals.h"
    6. #include "qtgui/GUIDados.h"
    7.  
    8. using namespace std;
    9.  
    10. namespace global2
    11. {
    12. int numberOfMotors;
    13. int intensidade[16];
    14. int local[16];
    15. }
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19.  
    20. QApplication a(argc, argv); //creates a QApplication object
    21. MainWindow w; //creates the main window object
    22. w.show();
    23.  
    24. cout << "test: " << global2::numberOfMotors << endl;
    25. return a.exec(); //enter its event loop
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    However, I always get test 0 and nothing else. In my mainwindow.cpp I am able to write those global variables in a label in my gui, like for example:
    (this is just an excerpt of my mainwindow.cpp code)

    Qt Code:
    1. #include "ros/ros.h"
    2.  
    3. #include "mainwindow.h"
    4. #include "ui_mainwindow.h"
    5. #include <QtGui/QPushButton>
    6. #include "globals.h"
    7. #include <QString>
    8. #include <string>
    9. #include <sstream>
    10. #include "QMessageBox"
    11.  
    12.  
    13. namespace global
    14. {
    15. int numberOfMotors;
    16. int intensidade[16];
    17. int local[16];
    18. }
    19.  
    20. using namespace std;
    21.  
    22. void MainWindow::on_sendButton_clicked()
    23. {
    24. QMessageBox::information(this,tr("Titulo"),tr("escolheu: %1").arg( global::intensidade[0]));
    25. ui->label1->setText(QString::number(global::numberOfMotors));
    26. }
    To copy to clipboard, switch view to plain text mode 

    Can you explain me what I'm doing wrong?

  2. #2
    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: Qt GUI print global variables

    Your main window's event loop isn't started until line 25 is executed in your main routine, so events are not processed until after you've already executed line 24 and printed out the value. BTW, global variables are evil and should be avoided at all costs...
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt GUI print global variables

    If this is your actual code and you really are declaring the variables in the global2 namespace twice, then the ones declared in main.cpp are not the same as the ones in MainWindow.cpp and so are never initialized. That the call to cout prints "0" and not gibberish is probably the result of a friendly compiler that initializes variables to zero in release mode for you.

    Time to read up on your basic C++ on variables and scoping. Here's a start.

    @jefftee: This has nothing to do with the event loop or event processing. cout is plain old C++. And unfortunately, Arduino and RasPi coding books have brought back the evil global variable with a vengeance. From the names of the variables, this seems to be what this is about.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    jefftee (27th April 2017)

  5. #4
    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: Qt GUI print global variables

    Quote Originally Posted by d_stranz View Post
    @jefftee: This has nothing to do with the event loop or event processing. cout is plain old C++. And unfortunately, Arduino and RasPi coding books have brought back the evil global variable with a vengeance. From the names of the variables, this seems to be what this is about.
    Thanks, I jumped to the wrong conclusion... I still feel that global variables are an indication that you have poorly designed your classes/interfaces and a crutch used by too many people just to make it work...

    Edit: after reviewing the code, he's creating the same variable names in two different namespaces (global and global2) so they are indeed different variables and I don't see where they are ever initialized either.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. The following user says thank you to jefftee for this useful post:

    d_stranz (27th April 2017)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt GUI print global variables

    he's creating the same variable names in two different namespaces (global and global2)
    Ah, and I missed that detail. Even if he does initialize the variables in the "global" namespace, the variables in "global2" remain uninitialized.

    And I agree about global variables also. The closest I ever come are static const variables in cpp module scope or singleton classes that can only have one instance. But as I said, pick up any Arduino or Raspberry Pi coding book and they are in every example. It might be the most straightforward way to teach coding for single board computers, but it teaches bad habits that don't scale well when used in real-world applications.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #6
    Join Date
    Apr 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt GUI print global variables

    Hi jefftee, d_stranz,

    Yes, the main goal of this program is to send this variables via message (ROS) to Arduino.

    I'm sorry, I forgot to post my globals.h file, that's where I initialize my global variables:

    Qt Code:
    1. #ifndef GLOBALS_H
    2. #define GLOBALS_H
    3.  
    4. extern int numberOfMotors;
    5. extern int intensidade[16];
    6. extern int local[16];
    7.  
    8. #endif // GLOBALS_H
    To copy to clipboard, switch view to plain text mode 

    As you can imagine I am very new in this C++ thing, I've only used arduino and C, so that's why I jumped right away to the global variables.

    I declared another namespace (global2) because when i tried to use the global namespace that I created in the other window it said that that namespace was already used (or something like that ). I thought I didnt even need to create that namespace because the variables should already be declared in the globals.h, however, if I only do numberOfMotors = 5 for example (I'm sorry if I'm saying something stupid), then I get an error saying that numberOfMotors doesnt name a type.

    Instead of using global variables should I try to use classes? The reason I tried to escape from it is that I'm not very sure how the all classes thing work, and I didn't want to mess things up even more than they already are.

    Thank you for the answers!!


    Added after 14 minutes:


    Yup, I'm very very noob on this.
    Turns out I only needed to initialize in my mainwindow.cpp the following
    Qt Code:
    1. int numberOfMotors;
    2. int intensidade[16];
    3. int local[16];
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. numberOfMotors =5;
    6. }
    To copy to clipboard, switch view to plain text mode 
    and in my main.cpp I just did
    Qt Code:
    1. cout << "exp " << numberOfMotors << endl;
    To copy to clipboard, switch view to plain text mode 
    without initializing anything and it printed 5.

    Now the thing is, it only prints once and i wanted it to constantly print it everytime I update something. I guess now it has to do with what jefftee said first..
    Last edited by jonagf; 27th April 2017 at 10:58.

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt GUI print global variables

    I'm sorry, I forgot to post my globals.h file, that's where I initialize my global variables:
    No, this is not initializing, it is telling the compiler that somewhere in the program there are variables named "numberOfMotors", etc. that have global visibility. Initializing means assigning an initial value to those variables. If you don't initialize them, then the compile can choose to (a) do nothing (in which case your program starts with the variables having whatever random value the memory happened to contain prior to that), (b) zero them out (i.e. set the memory to all zero bits), or (c) set them to some "known" value that you can recognize in the debugger as a default value for an uninitialized variable. It looks like your compiler might be doing option (b).

    Turns out I only needed to initialize in my mainwindow.cpp the following
    So, in globals.h you are telling the compiler that these variables are declared somewhere in the program. In these lines in mainwindow.cpp you are actually declaring them. Declararing is not initializing. In line 5 of your MainWindow constructor is where you are assigning a value to "numberOfMotors". Technically speaking, this is not initialization, since someone could access that variable prior to constructing the MainWindow instance and the variable would be uninitialized at that point. Your other two array variables are still uninitialized after the MainWindow constructor exits.

    The reason your cout statement in main.cpp works (i.e. prints "5") is because you placed the cout statement after the MainWindow instance "w" is declared (and constructed). If you put it before that statement, it would print "0" (or some random, uninitialized value).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. The following user says thank you to d_stranz for this useful post:

    jonagf (28th April 2017)

Similar Threads

  1. Replies: 1
    Last Post: 2nd September 2013, 23:52
  2. Are global variables evil?
    By Wonk0 in forum Newbie
    Replies: 3
    Last Post: 2nd February 2011, 21:11
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2007, 00:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 18:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 20:56

Tags for this Thread

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.