When I am trying to emit a signal from another thread it causes a segfault, not sure why.
Both the signal and slot are defined in the same class and running under the main GUI thread, but I call the emit in another function which is being controlled by a boost thread type of thread.
I am using Qt4 and Ubuntu 10.04 is my OS. This function is called from another thread which is emitting the signal.
void MyMapItem::updateMap(std::vector<int> data11)
{
my_mutex.lock();
cout<< "i am in updatemap"<<endl;
data12.clear();
data12=data11;
cout<<"size of data"<<data12.size()<<endl;
my_mutex.unlock();
emit mera_signal();
}
MyMapItem::MyMapItem(QGraphicsItem *parent )
{
QObject::connect(this,SIGNAL(mera_signal()),this,SLOT(mera_slot()),Qt::BlockingQueuedConnection );
}
Here above is my constructor of Qt class.
void MyMapItem::mera_slot()
{
cout<< "signal is emitted"<<endl;
qDebug() << "Date:";
}
And above is the slot definition I am just printing a message for the time being.
Let me elaborate my flow a little bit more.
- I have one class
MapGenerator
which is inherited fromQThread
now, which is connected to ROS and subscribing to a topic. - Now I get another class
MyMapitem
which is inherited fromQObject
andGraphicsItem
both and in this class I have defined a slot and a signal. - Now I got a third class
Mainwindow
which is inherited fromQobject
and setting up the graphicsscene for me takingmymapitem
. - Now what I do in
main
is to make object ofMapgenerator
and start the thread. - Then I make an object of
Mainwindow
. - So when
Mapgenerator
thread starts it subscribes data from ROS and calls a function inMyMapItem
and transfer data there.
Here I want to emit a signal so I know that new data arrived. Then I update the item which is already in the scene there in the Mainwindow
constructor. The connection is made in MyMapItem
class constructor.
Thanks Here i post my main method where i am creating the thread and main window.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ros::init(argc,argv,"last");
MapGenerator::MapGenerator mg(argc,argv);
//boost::thread ros_thread(boost::bind(&MapGenerator::init2, &mg));
mg.start(); // Qthread
MainWindow w(argc,argv);
w.show();
return a.exec();
}
here in Main window constructor i made Mapitem object
MainWindow::MainWindow( int argc, char **argv, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("My app");
mapitem = new MyMapItem();
scene = new QGraphicsScene(0,0,4000,4000);
ui->graphicsView->setScene(scene);
scene->addItem(mapitem);
}