If this is your first visit, be sure to
check out the
FAQ
by clicking the
link above. You may have to
register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
Welcome to
Qt Centre
.
Qt Centre
is a community site devoted to programming in C++ using the
Qt framework
. Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue —
register
and post your question.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please,
join our community today
!
If you have any problems with the registration process or your account login, please
contact us
.
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???
Qt Code:
Switch view
int main(int argc, char *argv[])
{
qDebug() << "Hallo";
return a.exec();
}
int main(int argc, char *argv[])
QCoreApplication a(argc, argv);
qDebug() << "Hallo";
return a.exec();
To copy to clipboard, switch view to plain text mode
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();
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
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "thrd_readfile.h"
#include "thrd_splitfile.h"
Q_OBJECT
public:
public slots:
signals:
};
int main(int argc, char *argv[])
{
qDebug() << "Hallo";
return a.exec();
}
#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();
To copy to clipboard, switch view to plain text mode
And now??
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?
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.
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "thrd_readfile.h"
#include "thrd_splitfile.h"
Q_OBJECT
public:
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[])
{
qDebug() << "Hallo";
MyObj* myThreadObject = new MyObj();
return a.exec();
}
#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();
To copy to clipboard, switch view to plain text mode
Is tis ok?
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:
Qt Code:
Switch view
int main(int argc, char *argv[])
{
MyObj myThreadHandler;
thread1 * mythread1 = new thread1();
connect(mythread1, SIGNAL(finished()), &myThreadHandler, SLOT(threadSplitFileFinished()));
return a.exec();
}
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();
To copy to clipboard, switch view to plain text mode
It all depends on how your application architecture is and where you want to store the pointer etc.
I compile the app with succes and it run also successfull.
The application don´t quit automatically.
Qt Code:
Switch view
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "mythreadobject.h"
int main(int argc, char *argv[])
{
MyThreadObject* StartMyThreads = new MyThreadObject(a.arguments().at(1));
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QObject>
#include "mythreadobject.h"
QCoreApplication a(argc, argv);
MyThreadObject* StartMyThreads = new MyThreadObject(a.arguments().at(1));
return a.exec();
To copy to clipboard, switch view to plain text mode
So what is the problem??
you have to call quit on your core application. Use qApp as a global pointer to it. So whenever you want to quit:
Qt Code:
Switch view
qApp->quit();
//or
qApp->quit();
QCoreApplication::instance()->quit();
To copy to clipboard, switch view to plain text mode