我正在使用具有QT4生成的GUI的开源代码。我想要做的是稍微更改代码,此时需要在GUI上按下按钮。我想要的是要调用的函数,这样我就不必按一个按钮来执行它。
该功能在公共时隙下的类tum_ardrone_gui中定义。该函数称为SendClicked(),因此我通过定义一个对象来调用该函数,如:
tum_ardrone_gui* gui;
gui->SendClicked();
我收到错误,所以我的语法错误或者我不允许这样调用'slot'定义的函数?
提前致谢!
编辑1:感谢大家的帮助。这是我的main.cpp文件:
#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;
}
答案 0 :(得分:1)
这里的主要问题是您只将gui
声明为指向tum_ardrone_gui
的指针,但实际上并未创建对象。相反,你应该做类似的事情:
tum_ardrone_gui gui = new tum_ardrone_gui();
gui->SendClicked();
但请务必在完成后删除gui
。例如,如果此代码位于窗口小部件中,则只需编写
tum_ardrone_gui gui = new tum_ardrone_gui(this);
分配gui
时,当此小部件消失时,它将被释放。
至于你的问题,从C ++的角度来看,Qt插槽只是一个常规方法,所以把它称为方法。