PDA

View Full Version : adding slots in console app



codeman
8th August 2010, 01:53
Hello Friends,

I write a little console app with a print output.
I also have a Thread class. My problem is where to define the slots for the Signal of the thread???



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

qDebug() << "Hallo";

return a.exec();
}

Where can I create my thread object and connect my signal to my slot.

For example for the THREAD::finished() Signal and my slot myThrdIsFinished();

borisbn
8th August 2010, 05:54
You should define a class, derived from QObject, create an object of this class before a.exec(), then you should create a thread-derived object, connect it's signals to first object's slots, call start() function of a thread's object and then call a.exec().
And take a mind, that in Qt you can't declare a QObject-derived class in *.cpp, but only in *.h

Lykurg
8th August 2010, 08:32
And take a mind, that in Qt you can't declare a QObject-derived class in *.cpp, but only in *.hYou can, you only have include the moc file by yourself.

codeman
9th August 2010, 12:17
ok.



#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "thrd_readfile.h"
#include "thrd_splitfile.h"
class MyObj: public QObject {
Q_OBJECT

public:
MyObj(QObject *parent = 0) : QObject(parent) {}
public slots:
signals:
};

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hallo";
return a.exec();
}


And now??

Lykurg
9th August 2010, 12:23
And now??And now what? We won't write your application. Read the documentation about signal and slots and TRY. If you then stuck, you can ask, but the code you posted is nothing.

codeman
9th August 2010, 14:05
What do you talkin about. I dont have the expectation that you write my application. I use thread many times in Gui apps. The only reason I write here is to ask how to do it, cause I never use it in console apps and I cannot figure out where to declare my slots etc.
Stop talking in such general terms. Ok it may be that I'm not as good as you, but that's why I do not expect you to write my application for me. I want to understand it, and a documentation sometimes has its limits and that everyone learns differently is obvious, I was hoping that one ever done this and possibly a snippet of disposal is. That is all. I do not want a finished application.

Thanks for all.... ;o?

Lykurg
9th August 2010, 19:27
There is no difference between using signal and slots in a GUI application or a console application. I a console application you normally also use classes. instead of inherit QWidget simple inherit QObject and add slots and signals to this classes. Fine and now you can connect them.

And I talk in general terms if you ask in general terms.

codeman
9th August 2010, 21:30
Thank you very much, but when you write:
We won't write your application. Read the documentation ....

This is general for me.

When I understand you right is this the right approach or?




#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "thrd_readfile.h"
#include "thrd_splitfile.h"
class MyObj: public QObject {
Q_OBJECT

public:
MyObj(QObject *parent = 0) : QObject(parent) {
thread1 * mythread1 = new thread1();
QObject::connect(thr_mythread1 , SIGNAL(finished()),this, SLOT(threadReadFileFinished()));
thread * mythread2 = new thread2();
QObject::connect(thr_mythread2 , SIGNAL(finished()),this, SLOT(threadSplitFileFinished()));
mythread1 ->start();
mythread2 ->start();
}
public slots:
void threadReadFileFinished() {
qDebug("Executing slot threadReadFileFinished()");
}
void threadSplitFileFinished() {
qDebug("Executing slot threadSplitFileFinished()");
}

signals:
};

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hallo";
MyObj* myThreadObject = new MyObj();
return a.exec();
}

Is tis ok?

Lykurg
9th August 2010, 21:59
That is one possibility. You also can create the threads in your main and only use the slot of your class MyObj.
Something like that:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyObj myThreadHandler;
thread1 * mythread1 = new thread1();
connect(mythread1, SIGNAL(finished()), &myThreadHandler, SLOT(threadSplitFileFinished()));
return a.exec();
} It all depends on how your application architecture is and where you want to store the pointer etc.

codeman
9th August 2010, 22:35
Ahh Thanks ;o))

codeman
10th August 2010, 00:08
But I have one problem.

I compile the app with succes and it run also successfull.

The application don´t quit automatically.



#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "mythreadobject.h"




int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThreadObject* StartMyThreads = new MyThreadObject(a.arguments().at(1));
return a.exec();
}


So what is the problem??

Lykurg
10th August 2010, 06:41
you have to call quit on your core application. Use qApp as a global pointer to it. So whenever you want to quit:
qApp->quit();
//or
QCoreApplication::instance()->quit();

codeman
11th August 2010, 00:20
Thank you very much I gave the reference from the QCoreApplication of my app to my Threadobject and quit it there and it runs ok ;o))