我正在使用由QT4生成的GUI的開源代碼。我想要做的就是稍微改變一下代碼,這時它需要在GUI上按下一個按鈕。我想要的是被調用的函數,所以我不必按下按鈕來執行它。如何調用一個插槽的qt函數?
該函數在public slots中的tum_ardrone_gui類中定義。該功能被稱爲SendClicked(),所以我被定義對象,如調用函數:
tum_ardrone_gui* gui;
gui->SendClicked();
我得到一個錯誤,所以要麼是我的語法錯誤還是我不準叫「槽」中定義的功能,如這個?
在此先感謝!
編輯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;
}
你有什麼錯誤? – khajvah
@ khajvah:可能是Segfault。他沒有創建'tum_ardrone_gui'對象。 –
@VioletGiraffe是的,對。不能相信我錯過了它:D – khajvah