Solved New signal slot syntax attempt
-
Hi! I'm trying to implement new signal slot syntax. Documentation is [here].(http://wiki.qt.io/New_Signal_Slot_Syntax)
My aim is to connect signal from object to simple function. I get error:main.cpp:14: error: no matching function for call to 'QObject::connect(SigGen&, void (SigGen::*)(), void (&)())' QObject::connect(siggen, &SigGen::sgAction, handler);
My code:
main.cpp:#include <QCoreApplication> #include <iostream> #include <QObject> #include "siggen.h" void handler(){ std::cout << "got signal" << std::endl; } int main(int argc, char *argv[]) { SigGen siggen; QObject::connect(siggen, &SigGen::sgAction, handler); siggen.action(); QCoreApplication a(argc, argv); std::cout << "main prog start" << std::endl; return a.exec(); }
siggen.h:
#ifndef SIGGEN_H #define SIGGEN_H #include <QObject> class SigGen : public QObject { Q_OBJECT public: explicit SigGen(QObject *parent = 0); void action(void); signals: void sgAction(void); public slots: }; #endif // SIGGEN_H
siggen.cpp:
#include "siggen.h" SigGen::SigGen(QObject *parent) : QObject(parent) { } void SigGen::action() { emit sgAction(); }
-
hi,
connect(sender, &Sender::valueChanged, someFunction);
sender needs to be a pointer to your object.
So in your case try one of the following changes:SigGen *siggen = new SigGen(this); // or QObject::connect(&siggen, &SigGen::sgAction, handler);
-
@J.Hilk Thanks!
-
Hi,
Some additions to @J-Hilk,
- always create your QCore/Gui/Application as the first thing you do in your application. It setups Qt's internal so it will work properly.
- In your main function, if you allocated
siggen
on the heap, then delete it before returning the result ofapp.exec()
for proper cleanup. Otherwise pass it your QCoreApplication object as parent.