Question

I am working with open source code that has a GUI generated by QT4. What I want to do is change the code slightly where at the moment it requires a button pressed on the GUI. What I want is the function to be called so that I do not have to press a button to execute it.

The function is defined in the class tum_ardrone_gui under public slots. The function is called SendClicked() so I was calling the function by defining an object such as:

tum_ardrone_gui* gui;
gui->SendClicked();

I get an error so is either my syntax wrong or am I not allowed to call 'slots' defined functions like this?

Thanks in advance!

EDIT 1: Thanks for the help everyone. Here is the my main.cpp file:

#include "../UINode/tum_ardrone_gui.h"
#include "../UINode/RosThread.h"
#include "../UINode/PingThread.h"

#include <QtGui>
#include <QApplication>
#include "ros/ros.h"

// this global var is used in getMS(ros::Time t) to convert to a consistent integer timestamp used internally pretty much everywhere.
// kind of an artifact from Windows-Version, where only that was available / used.
unsigned int ros_header_timestamp_base = 0;

int main(int argc, char *argv[])
{
std::cout << "Starting drone_gui Node" << std::endl;

// ROS
ros::init(argc, argv, "drone_guiuno");
RosThread t;
PingThread p;

// UI
QApplication a(argc, argv);
tum_ardrone_gui w;

// make them communicate with each other
t.gui = &w;
w.rosThread = &t;
p.gui = &w;
p.rosThread = &t;
w.pingThread = &p;

// start them.
t.startSystem();
p.startSystem();
w.show();


// Error 1):
//tum_ardrone_gui gui = new tum_ardrone_gui(); 
//gui->SendClicked();
//delete gui;

// DOES NOT COMPILE: ERROR MESSAGE /usr/include/qt4/QtGui/qwidget.h: In copy constructor ‘tum_ardrone_gui::tum_ardrone_gui(const tum_ardrone_gui&)’:
// /usr/include/qt4/QtGui/qwidget.h:806:5: error: ‘QWidget::QWidget(const QWidget&)’ is private

//Error 2): Compiles and works! But why didn't the above?
w.SendClicked();


// wait until windows closed....
int ec = a.exec();

 // stop ROS again....
t.stopSystem();
p.stopSystem();

std::cout << "Exiting drone_gui Node" << std::endl;

return ec;
}
Était-ce utile?

La solution

The main problem here is that you only declared gui as a pointer to tum_ardrone_gui, but didn't actually create an object. Instead, you should do something like:

tum_ardrone_gui gui = new tum_ardrone_gui();
gui->SendClicked();

But be sure to delete gui when you're done with it. For instance, if this code is within a widget, simply write

tum_ardrone_gui gui = new tum_ardrone_gui(this);

when allocating gui and then it will be freed when this widget dies.

As for your question, from C++ standpoint a Qt slot is just a regular method, so call it like a method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top