添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
冷静的日记本  ·  Tajikistan | UPR info·  2 月前    · 
逆袭的大象  ·  java调用动态库异常 ...·  8 月前    · 
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
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication a(argc, argv);
  4.  
  5. qDebug() << "Hallo";
  6.  
  7. return a.exec();
  8. }
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
  1. #include <QtCore/QCoreApplication>
  2. #include <QDebug>
  3. #include <QObject>
  4. #include "thrd_readfile.h"
  5. #include "thrd_splitfile.h"
  6. class MyObj: public QObject {
  7. Q_OBJECT
  8.  
  9. public:
  10. MyObj(QObject *parent = 0) : QObject(parent) {}
  11. public slots:
  12. signals:
  13. };
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. QCoreApplication a(argc, argv);
  18. qDebug() << "Hallo";
  19. return a.exec();
  20. }
#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.
  1. #include <QtCore/QCoreApplication>
  2. #include <QDebug>
  3. #include <QObject>
  4. #include "thrd_readfile.h"
  5. #include "thrd_splitfile.h"
  6. class MyObj: public QObject {
  7. Q_OBJECT
  8.  
  9. public:
  10. MyObj(QObject *parent = 0) : QObject(parent) {
  11. thread1 * mythread1 = new thread1();
  12. QObject::connect(thr_mythread1 , SIGNAL(finished()),this, SLOT(threadReadFileFinished()));
  13. thread * mythread2 = new thread2();
  14. QObject::connect(thr_mythread2 , SIGNAL(finished()),this, SLOT(threadSplitFileFinished()));
  15. mythread1 ->start();
  16. mythread2 ->start();
  17. }
  18. public slots:
  19. void threadReadFileFinished() {
  20. qDebug("Executing slot threadReadFileFinished()");
  21. }
  22. void threadSplitFileFinished() {
  23. qDebug("Executing slot threadSplitFileFinished()");
  24. }
  25.  
  26. signals:
  27. };
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. QCoreApplication a(argc, argv);
  32. qDebug() << "Hallo";
  33. MyObj* myThreadObject = new MyObj();
  34. return a.exec();
  35. }
#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
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication a(argc, argv);
  4. MyObj myThreadHandler;
  5. thread1 * mythread1 = new thread1();
  6. connect(mythread1, SIGNAL(finished()), &myThreadHandler, SLOT(threadSplitFileFinished()));
  7. return a.exec();
  8. }
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
  1. #include <QtCore/QCoreApplication>
  2. #include <QDebug>
  3. #include <QObject>
  4. #include "mythreadobject.h"
  5.  
  6.  
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QCoreApplication a(argc, argv);
  12. MyThreadObject* StartMyThreads = new MyThreadObject(a.arguments().at(1));
  13. return a.exec();
  14. }
#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
  1. qApp->quit();
  2. //or
  3. QCoreApplication::instance()->quit();
qApp->quit();
QCoreApplication::instance()->quit();
To copy to clipboard, switch view to plain text mode