PDA

View Full Version : No such slot QCoreApplication ...



CraigD
8th July 2013, 23:17
I am trying to subclass QCoreApplication to execute various tasks that I define as part of a task class. The tasks have a "progress()" signal which I want to forward to the application, which can then report progress on its task to the console. I have the following code in MyConsoleApp.h


class MyConsoleApp: public QCoreApplication
{
Q_OBJECT
public:
MyConsoleApp(int &argc, char **argv);
void setTask(MyTask *my_task,
bool exit_on_completion = true,
bool show_progress=false);
void executeTask();
public slots:
void showProgress(int i);
private:
MyTask* m_task;
};

And in MyConsoleApp.cpp (MyTask is a subclass of QRunnable, it emits finished() when done and a progress(int) signal during processing):


void MyConsoleApp::setTask(MyTask *my_task, bool exit_on_completion, bool show_progress) {
m_task = my_task;

if(m_task && exit_on_completion) {
QObject::connect(m_task, SIGNAL(finished()),
this, SLOT( quit() ));
}
if(m_task && progress) {
QObject::connect( m_task, SIGNAL(progress(int)),
this, SLOT(showProgress(int)));
}

}

void MyConsoleApp::executeTask() {
if(m_task) {
QThreadPool::globalInstance()->start(m_task);
}

void MyConsoleApp::showProgress(int i) {
std::cout << i << "% complete." << std::endl;
}


Then my main function is:


int main(int argc, char *argv[])
{
MyConsoleApp a(argc, argv);

a.setTask( new MyTask(), true, true);
a.executeTask();

return a.exec();
}


Sorry, this is not a compilable example. I tried to reduce it to one, and ended up with an even more strange error.

Anyway, everything seems to work. My task is executed and runs to completion, and the application exits when the task is done. However, the progress is never displayed and I get the following error message when I run the program:

QObject::connect: No such slot QCoreApplication::showProgress(int) in MyApp.cpp

Which I cannot understand because the slot is there and they are connected. It seems like it is looking for a SLOT showProgress in the superclass (QCoreApplication) rather in the class which I have defined MyConsoleApp - and I am not sure why that happens.

Any help would be appreciated.

Regards,
Craig

ChrisW67
9th July 2013, 03:24
The file containing your MyConsoleApp declaration is listed in the PRO file HEADERS and qmake has been run since it was added?

CraigD
9th July 2013, 13:32
The file containing your MyConsoleApp declaration is listed in the PRO file HEADERS and qmake has been run since it was added?

That must have been the problem. I created the program in QtCreator and when I checked the .pro file everything was there. However I tried restarting Creator and did a full rebuild
and that fixed whatever the problem was. I guess the .pro file and Makefile got out of sync perhaps.

That is one of the disadvantages of using a IDE I guess. I can save you work at times, but it also hides details about the build process, making them easy to forget.

Thanks for your help.

Craig