Can you help me for not work in signals problem.
-
Hello.
I think it's beginner's problem.. but i'm not solve this problem in 5 hours.this code is console source and error message is shown.
main.obj:-1: error: LNK2019: "public: void __cdecl EndChecking::quitSignal(void)" (?quitSignal@EndChecking@@QEAAXXZ) external sign (reference location: "protected: virtual void __cdecl EndChecking::run(void)" (?run@EndChecking@@MEAAXXZ) function )에서 확인하지 못했습니다.
how do i modify it? i can't understand where error code is.
Please help me for this situations.Thx.
in source is below
#include <QCoreApplication>
#include <QThread>
#include <iostream>class EndChecking : public QThread
{
Q_OBJECT
protected:
virtual void run() override;signals: void quitSignal();
};void EndChecking::run()
{
char c = NULL;while (true) { std::cin >> c; if (c == 'q' || c == 'Q') emit quitSignal(); }
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);std::cout << "type Q to exit Program" << std::endl; EndChecking* pchk = new EndChecking(); QObject::connect(pchk, SIGNAL(quitSignal()), &a, SLOT(quit())); pchk->start(); return a.exec();
}
-
Hi and welcome to devnet,
Did you put that all in a single file ?
-
@jsulm Wow Thx.. it is worked.
But, i don't understand why this dvede in different cpp n h files.
I think that (devide and combine classes ) is same.
Can you help me why combine file is not work?
First of all, thak you for solving my problem.
Thx. -
@nyamnyam The reason for that is that QObject based classes needs to be parsed by moc Qt tool which generates code for signals/slots. See https://doc.qt.io/qt-5/why-moc.html
-
If you have everything in a single file you need to add:
#include "main.moc"
At the bottom of the file and re-run qmake so that moc is run on that cpp file and thus the corresponding code is generated.
-
@jsulm said in Can you help me for not work in signals problem.:
Move EndChecking to its own header and cpp file
The reason for that is that QObject based classes needs to be parsed by moc Qt tool which generates code for signals/slotsAh ha! :) Now that I am doing C++ with Qt: could you be specific about what requirements/restrictions this imposes? Does this mean, say, that you cannot have two classes derived from
QObject
in the same.cpp
/.h
as one another? -
No particular restrictions. As written before, QObject based classes are usually declared in their own header. Nothing prevents you from having them in a cpp file, you just have to put the corresponding include at the end so they are also parsed by moc for QObject based classes in them.