Results 1 to 7 of 7

Thread: Qt GUI print global variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.

Similar Threads

  1. Replies: 1
    Last Post: 2nd September 2013, 22:52
  2. Are global variables evil?
    By Wonk0 in forum Newbie
    Replies: 3
    Last Post: 2nd February 2011, 20:11
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19: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.