Results 1 to 13 of 13

Thread: adding slots in console app

  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default adding slots in console app

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

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. qDebug() << "Hallo";
    6.  
    7. return a.exec();
    8. }
    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();

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: adding slots in console app

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: adding slots in console app

    Quote Originally Posted by borisbn View Post
    And take a mind, that in Qt you can't declare a QObject-derived class in *.cpp, but only in *.h
    You can, you only have include the moc file by yourself.

  4. #4
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

    ok.

    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 

    And now??

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: adding slots in console app

    Quote Originally Posted by codeman View Post
    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.

  6. #6
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: adding slots in console app

    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.

  8. #8
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

    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?

    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 

    Is tis ok?

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: adding slots in console app

    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:
    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. }
    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.

  10. The following user says thank you to Lykurg for this useful post:

    codeman (9th August 2010)

  11. #10
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

    Ahh Thanks ;o))

  12. #11
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

    But I have one problem.

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

    The application don´t quit automatically.

    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 

    So what is the problem??

  13. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: adding slots in console app

    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:
    1. qApp->quit();
    2. //or
    3. QCoreApplication::instance()->quit();
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: adding slots in console app

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

Similar Threads

  1. QT Console app
    By yorkshireflatcap in forum Newbie
    Replies: 14
    Last Post: 22nd June 2010, 20:57
  2. Dynamic widgets adding and connecting signals& slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2009, 12:36
  3. Why there is not console window?
    By HelloDan in forum Qt Programming
    Replies: 2
    Last Post: 8th April 2009, 16:02
  4. No console output in Mac OSX using Qt4
    By popoholic in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2006, 01:36
  5. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.