Cann't write connect in main.cpp
-
#include <QCoreApplication> #include <QTimer> #include <QObject> #include <QDebug> class Taz742 : public QObject { public: Taz742() { tm = new QTimer(); } QTimer *tm; ~Taz742() { delete tm; } public slots: void Print() { qDebug() << "Taz742"; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Taz742 *L = new Taz742(); L->tm->start(1000); L->connect(L->tm, SIGNAL(timeout()), L, SLOT(Print())); return a.exec(); }
on the black screen appears --> QObject::connect No such slot QObject::Print()
what is wrong in my code? -
#include <QCoreApplication> #include <QTimer> #include <QObject> #include <QDebug> class Taz742 : public QObject { public: Taz742() { tm = new QTimer(); } QTimer *tm; ~Taz742() { delete tm; } public slots: void Print() { qDebug() << "Taz742"; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Taz742 *L = new Taz742(); L->tm->start(1000); L->connect(L->tm, SIGNAL(timeout()), L, SLOT(Print())); return a.exec(); }
on the black screen appears --> QObject::connect No such slot QObject::Print()
what is wrong in my code? -
class Taz742 : public QObject { Q_OBJECT public: Taz742() { tm = new QTimer(); } QTimer *tm; ~Taz742() { delete tm; } public slots: void Print() { qDebug() << "Taz742"; } };
Thanks,
-
-
@Pradeep-Kumar
of course :))undefined reference to 'vtable for Taz742'
-
class Taz742 : public QObject { Q_OBJECT public: Taz742() { tm = new QTimer(); } QTimer *tm; ~Taz742() { delete tm; } public slots: void Print() { qDebug() << "Taz742"; } };
Thanks,
-
@Pradeep-Kumar
of course :))undefined reference to 'vtable for Taz742'
-
Hi @L.Gogs
Your Taz742 destructor is virtual, by inheritance from QObject. A class with virtual functions should be defined in its own file: Taz742.cpp and Taz742.h
-Michael.
-
@L.Gogs said in Cann't write connect in main.cpp:
no no, I want to write connect into main.cpp without any other header or cpp file.
You can hack it but using a .h file is the right way and besides this approach might have issues.
I never used it as its non common to only have .cpp file outside school projects.