Signal and slot problem
-
i am trying to connect my signals and slots but it's not working codes are below
void gpio::ISR() { gpio GPIO; if(gpio::isHIGH()) { qDebug()<<"RISING Interrupt called"; GPIO.emit_RisingSignal(); } else if(gpio::isLOW()) { qDebug() <<"FALLING edge"; GPIO.emit_FallingSignal(); } } void gpio::emit_RisingSignal() { qDebug()<<"In Rising Function."; emit RisingEdge(); } void gpio::emit_FallingSignal() { qDebug()<<"In Falling Function"; emit FallingEdge(); }
and in Header file
#include <QObject> class gpio : public QObject { Q_OBJECT public: gpio(); void static Initialize(); void static ISR(); bool static isHIGH(); bool static isLOW(); void emit_RisingSignal(); void emit_FallingSignal(); private: static const int POWER=21; static const int BUTTON=20; signals: void RisingEdge(void); void FallingEdge(void); };
And in MainWindow.cpp
gpio* GPIO=new gpio(); connect(GPIO,SIGNAL(RisingEdge()),this,SLOT(inserted())); connect(GPIO,SIGNAL(FallingEdge()),this,SLOT(Removed())); void MainWindow::inserted() { qDebug()<<"Inserted..."; } void MainWindow::Removed() { qDebug()<<"Removed..."; }
and in MainWindow.h
public slots: void inserted(); void Removed();
When i Run this program i Get
RISING Interrupt called
In Rising Function.
FALLING edge
In Falling Functionbut it should be
RISING Interrupt called
In Rising Function.
Inserted....
FALLING edge
In Falling Function
Removed...So clearly the slots are not being called i can't figure out the problem and Help is badly needed.
-
qDebug()<<"Connection Rising edge: "<<connect(GPIO,SIGNAL(RisingEdge()),this,SLOT(inserted())); qDebug()<<"Connection falling edge:"<<connect(GPIO,SIGNAL(FallingEdge()),this,SLOT(Removed()));
gives the output
Connection Rising edge: true
Connection falling edge: true -
@azravian said:
ok super so they accept the connect.your error is here
void gpio::ISR()
{
gpio GPIO; <<<<<<<<<<<<<< that is not the one u connect !?That is not the
gpio* GPIO=new gpio();
one :)Update:
So when you do
GPIO.emit_FallingSignal();
with your local copy, no signal is sent to mainwin as
its an other instance than the one you new in main. -
@mrjj that's seems to be a problem but if i change my ISR()
void gpio::ISR() { gpio* GPIO=new gpio; if(gpio::isHIGH()) { qDebug()<<"RISING Interrupt called"; GPIO->emit_RisingSignal(); } else if(gpio::isLOW()) { qDebug() <<"FALLING edge"; GPIO->emit_FallingSignal(); } } i have the same problem, it didn't solve the problem/
-
but why are u creating a new Object???
Use the one in/from main.That is the one you have connect to mains slots so making a new one
in that function will not send any signals to main as you do not connect it
like the other one.So just newing the local copy instead, will not make any difference :)
-
@mrjj Sir, ISR() is a static function. So to access other functions of the class i need to create an object :) thats why i have defined two other function emit_RisingEdge() and emit_FallingEdge() so i can emit my signals independent of any object.
-
Hi,
Just one thing, you have now a memory leak.
But in any case, I must say I don't see any reason for making all these methods static.
-
-
@azravian In this case you could add a public static method to get the pointer to the gpio instance (basically you create a singleton):
class gpio : public QObject { Q_OBJECT public: gpio(): instancePtr(nullptr) {} static gpio* instance() { if (!instancePtr) instancePtr = new gpio(); return instancePtr; } private: gpio* instancePtr;
You can use this method in main to setup the connection and in static methods in gpio to get the pointer to the instance. Don't forget to delete the instance later.