Results 1 to 8 of 8

Thread: Do i have to inherit from QTcpServer?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Do i have to inherit from QTcpServer?

    Hi,

    I’m trying to create a threaded server and in every example I saw someone creates a server by inheriting from QTcpServer. So I wonder, do I have to? I mean, you inherit only if you want to extend the class.

    Here is the class:
    myserver.h
    Qt Code:
    1. #ifndef MYSERVER_H
    2. #define MYSERVER_H
    3.  
    4. #include <QObject>
    5. #include <QtNetwork/QTcpServer>
    6. #include <QtConcurrentRun>
    7. #include <QFuture>
    8. #include "myclienthandle.h"
    9.  
    10. class MyServer : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit MyServer(QObject *parent = 0, const QString &ip = "Any", int port = 0);
    15. explicit MyServer(const MyServer &server);
    16. ~MyServer();
    17. QString IP() const;
    18. void setIP(const QString &ip);
    19. int Port() const;
    20. void setPort(int port);
    21. bool isAlive() const;
    22.  
    23.  
    24. private:
    25. QTcpServer *m_qtcpserListener;
    26. QFuture<void> m_qftrRun;
    27. QString *m_qstrIP;
    28. int m_iPort;
    29. int m_iClientID;
    30. bool m_bRun;
    31.  
    32. void run();
    33.  
    34. signals:
    35. void newConnection(MyClientHandle *clientHandle);
    36.  
    37. public slots:
    38. void start();
    39. void stop();
    40. };
    41.  
    42. inline QString MyServer::IP() const
    43. {
    44. return *m_qstrIP;
    45. }
    46.  
    47. inline void MyServer::setIP(const QString &ip)
    48. {
    49. m_qstrIP = new QString(ip);
    50. }
    51.  
    52. inline int MyServer::Port() const
    53. {
    54. return m_iPort;
    55. }
    56.  
    57. inline void MyServer::setPort(int port)
    58. {
    59. m_iPort = port;
    60. }
    61.  
    62. inline bool MyServer::isAlive() const
    63. {
    64. return m_bRun;
    65. }
    66.  
    67. #endif // MYSERVER_H
    To copy to clipboard, switch view to plain text mode 

    myserver.cpp
    Qt Code:
    1. #include "myserver.h"
    2.  
    3. MyServer::MyServer(QObject *parent, const QString &ip, int port) :
    4. QObject(parent), m_iClientID(0), m_bRun(false)
    5. {
    6. setIP(ip);
    7. setPort(port);
    8. }
    9.  
    10. MyServer::MyServer(const MyServer &server)
    11. {
    12. MyServer(server.parent(), server.IP(), server.Port());
    13. }
    14.  
    15. MyServer::~MyServer()
    16. {
    17. stop();
    18. }
    19.  
    20. void MyServer::run()
    21. {
    22. m_qtcpserListener = new QTcpServer(this);
    23.  
    24. if (!m_qtcpserListener->listen(QHostAddress(IP()), Port()))
    25. {
    26. return;
    27. }
    28.  
    29. while (m_bRun)
    30. {
    31. QTcpSocket *qtcpsoNextPending = m_qtcpserListener->nextPendingConnection();
    32. if(qtcpsoNextPending != 0)
    33. {
    34. emit newConnection(new MyClientHandle(qtcpsoNextPending));
    35. }
    36. }
    37. }
    38.  
    39. void MyServer::start()
    40. {
    41. m_bRun = true;
    42. m_qftrRun = QtConcurrent::run(this, &MyServer::run);
    43. }
    44.  
    45. void MyServer::stop()
    46. {
    47. m_bRun = false;
    48. if (m_qtcpserListener->isListening())
    49. {
    50. m_qtcpserListener->close();
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 

    Greats p.kreker
    Last edited by p.kreker; 16th June 2011 at 13:44.

Similar Threads

  1. How can we inherit ui?
    By vinayaka in forum Newbie
    Replies: 5
    Last Post: 30th May 2011, 14:28
  2. About inherit
    By nesson in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2011, 13:07
  3. Inherit from QTabWidget
    By Suncell in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 21:06
  4. How to use the ui_*.h,inherit it or as a member?
    By 75543255 in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2009, 10:45
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Tags for this Thread

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.