Results 1 to 5 of 5

Thread: Threads creatoin and execution?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Threads creatoin and execution?

    Using Qt-4.1.1.

    I have looked at the documentation, and there is an exameple on how to create a thread inheriting from QThread. The problem is that I cannot see how to apply the example in this class:
    _______________________________________________--
    # Server.h
    # ifndef SERVER_H
    # define SERVER_H

    # include <QObject>
    # include "ServerGUI.h"

    class Server : public QObject
    {
    Q_OBJECT

    private:

    public:
    void run();

    public slots:
    void new_client();
    };

    # endif
    ________________________________________________--

    # Server.cpp
    # include <iostream>

    # include "Server.h"

    Server :: Server()
    {

    };


    void Server :: new_client()
    {
    std::cout << "New client connected\n";
    }

    void Server :: run()
    {
    std::cout << "Thread running\n";
    QThread::start(QThread::NormalPriority=QThread::In heritPriority);
    }
    _______________________________________________-

    There is some more code about connecting the server and so on, but is not relevant now.

    How do I start the thread ?

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Threads creatoin and execution?

    Your class must be inherit from QThread instead QObject and you also should reinplement protected method run() where must bee your needed for threading code. Read documentation !
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads creatoin and execution?

    I read the documentation.

    QThread inherits from QObject, threfore I assume that I do not need to inherit from QThread again.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Threads creatoin and execution?

    then read C++ documentation

    Qt Code:
    1. /// Server.h
    2. # ifndef SERVER_H
    3. # define SERVER_H
    4.  
    5. # include <QThread>
    6. # include "ServerGUI.h"
    7.  
    8. class Server : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. protected:
    13. void run();
    14.  
    15. public slots:
    16. void new_client();
    17. };
    18.  
    19. # endif
    20.  
    21.  
    22. /// Server.cpp
    23. # include <iostream>
    24. # include "Server.h"
    25.  
    26. Server :: Server()
    27. {
    28.  
    29. };
    30.  
    31.  
    32. void Server :: new_client()
    33. {
    34. std::cout << "New client connected\n";
    35. }
    36.  
    37. void Server :: run()
    38. {
    39. std::cout << "Thread running\n";
    40. }
    To copy to clipboard, switch view to plain text mode 

    Then after you create object Server call method start() and your thread will be started
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Threads creatoin and execution?

    Quote Originally Posted by probine
    QThread inherits from QObject, threfore I assume that I do not need to inherit from QThread again.
    Then you assume wrong. If QObject inherited QThread then this would be true, but not the other way round. All panda bears are animals but not all animals are panda bears, therefore "inheriting" an animal doesn't make you a panda bear (but inheriting a panda bear does make you an animal).

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.