PDA

View Full Version : Link method to Qpushbutton



jorgeg922
5th February 2017, 01:36
I have these 3 files below. Basically, I made a method in file LeptonTemp.cpp which I would like to link to a QPushButton. I instantiated the class into my main.cpp file, but when I try to place my method (doSomething) in SLOT, Im getting an error at compilation. Can anyone tell me what I am doing wrong?

LeptonTemp.cpp


LeptonTemp::LeptonTemp(){}
LeptonTemp::~LeptonTemp(){}
void LeptonTemp::doSomething(){
qDebug<<"Hello World";
}

LeptonTemp.h


class LeptonTemp
{
public:
LeptonTemp();
~LeptonTemp();
public slots:
void doSomething();
};


main.cpp


int main (int argc, char **argv)
{
QApplication a(argc, argv);
QWdiget *myWidget = new QWidget;
myWidget->setGeometry(400,300,340,290);

QPushButton *internalTmp = new PushButton("Internal temp", myWidget);
internalTmp->setGeometry(320,290,100,30);

LeptonTemp *temp = new LeptonTemp();
QObject::connect(internalTmp, SIGNAL(clicked()), temp, SLOT(doSomething)));

myWidget->show();
return a.exec;
}

anda_skoa
5th February 2017, 08:06
If you are using macro based connect(), i.e. the connect() overloads for the SIGNAL and SLOT macros, then the receiver needs to be a QObject derived class and have the Q_OBJECT marker



class LeptonTemp : public QObject
{
Q_OBJECT
public:


Or use "pointer to member function" connect() overload.

Cheers,
_