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:
#include "ros/ros.h"
#include "mainwindow.h"
#include <QApplication>
#include "globals.h"
#include "qtgui/GUIDados.h"
using namespace std;
namespace global2
{
int numberOfMotors;
int intensidade[16];
int local[16];
}
int main(int argc, char *argv[])
{
MainWindow w; //creates the main window object
w.show();
cout << "test: " << global2::numberOfMotors << endl;
return a.exec(); //enter its event loop
}
#include "ros/ros.h"
#include "mainwindow.h"
#include <QApplication>
#include "globals.h"
#include "qtgui/GUIDados.h"
using namespace std;
namespace global2
{
int numberOfMotors;
int intensidade[16];
int local[16];
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //creates a QApplication object
MainWindow w; //creates the main window object
w.show();
cout << "test: " << global2::numberOfMotors << endl;
return a.exec(); //enter its event loop
}
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)
#include "ros/ros.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui/QPushButton>
#include "globals.h"
#include <QString>
#include <string>
#include <sstream>
#include "QMessageBox"
namespace global
{
int numberOfMotors;
int intensidade[16];
int local[16];
}
using namespace std;
void MainWindow::on_sendButton_clicked()
{
QMessageBox::information(this,tr
("Titulo"),tr
("escolheu: %1").
arg( global
::intensidade[0]));
ui
->label1
->setText
(QString::number(global
::numberOfMotors));
}
#include "ros/ros.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui/QPushButton>
#include "globals.h"
#include <QString>
#include <string>
#include <sstream>
#include "QMessageBox"
namespace global
{
int numberOfMotors;
int intensidade[16];
int local[16];
}
using namespace std;
void MainWindow::on_sendButton_clicked()
{
QMessageBox::information(this,tr("Titulo"),tr("escolheu: %1").arg( global::intensidade[0]));
ui->label1->setText(QString::number(global::numberOfMotors));
}
To copy to clipboard, switch view to plain text mode
Can you explain me what I'm doing wrong?
Bookmarks