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
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