• 4046阅读
  • 1回复

請問各位高手emit的問題 [复制链接]

上一主题 下一主题
离线sdaaron
 

只看楼主 倒序阅读 楼主  发表于: 2009-08-12
小弟最近由書本範例改自己想要的聊天室程式
遇到一個問題,就是使用emit去呼叫,但都沒作用
由rtu_server.cpp中的emit send2Client(txtSend,length);
但都沒反應~
請各位高手幫我看看看哪裡出問題~感謝 !

  1. //rtu_server.h
  2. #ifndef RTU_SERVER_H
  3. #define RTU_SERVER_H
  4. #include <QtGui/QWidget>
  5. #include "ui_rtu_server.h"
  6. #include "server.h"
  7. class RTU_Server : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. RTU_Server(QWidget *parent = 0);
  12. ~RTU_Server();
  13. public:
  14. QListWidget *ListWidgetContent;
  15. QLabel* LabelPort;
  16. QLineEdit* LineEditPort;
  17. QPushButton* PushButtonCreate;
  18. int port;
  19. Server *server;
  20. QString txtSend;
  21. signals:
  22. void send2Client(QString,int);
  23. public slots:
  24. void slotCreateServer();
  25. void updateServer(QString,int);
  26. void slotSend2Client();
  27. private:
  28. Ui::RTU_ServerClass ui;
  29. };
  30. #endif
  31. //rtu_server.cpp
  32. #include "rtu_server.h"
  33. #include "server.h"
  34. RTU_Server::RTU_Server(QWidget *parent)
  35. : QWidget(parent)
  36. {
  37. ui.setupUi(this);
  38. connect(ui.PushButtonCreate,SIGNAL(clicked()),this,SLOT(slotCreateServer()));
  39. port = 8010;
  40. ui.LineEditPort->setText(QString::number(port));
  41. ui.PushButtonSend->setEnabled(false);
  42. connect(ui.PushButtonSend,SIGNAL(clicked()),this,SLOT(slotSend2Client()));
  43. }
  44. RTU_Server::~RTU_Server()
  45. {
  46. }
  47. void RTU_Server::slotCreateServer()
  48. {
  49. server = new Server(this,port);
  50. connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int)));
  51. ui.PushButtonCreate->setEnabled(false);
  52. ui.PushButtonSend->setEnabled(true);
  53. }
  54. void RTU_Server::updateServer(QString msg,int length)
  55. {
  56. ui.ListWidgetContent->addItem (msg.left(length) );
  57. }
  58. void RTU_Server::slotSend2Client()
  59. {
  60. txtSend = "Server:" + ui.LineEditSend->text();
  61. int length = 0;
  62. emit send2Client(txtSend,length);
  63. ui.LineEditSend->clear();
  64. ui.ListWidgetContent->addItem (txtSend);
  65. }
  66. //server.h
  67. #ifndef SERVER_H
  68. #define SERVER_H
  69. #include <QtNetwork>
  70. #include "tcpclientsocket.h"
  71. class Server : public QTcpServer
  72. {
  73. Q_OBJECT
  74. public:
  75. Server(QObject *parent = 0,int port=0);
  76. QList<TcpClientSocket*> tcpClientSocketList;
  77. signals:
  78. void updateServer(QString,int);
  79. public slots:
  80. void updateClients(QString,int);
  81. void slotDisconnected(int);
  82. void send2Client(QString,int);
  83. protected:
  84. void incomingConnection(int socketDescriptor);
  85. };
  86. #endif
  87. //server.cpp
  88. #include <QtNetwork>
  89. #include "server.h"
  90. Server::Server(QObject *parent,int port)
  91. : QTcpServer(parent)
  92. {
  93. listen(QHostAddress::Any,port);
  94. }
  95. void Server::incomingConnection(int socketDescriptor)
  96. {
  97. TcpClientSocket *tcpClientSocket = new TcpClientSocket(this);
  98. connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int)));
  99. connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));
  100. tcpClientSocket->setSocketDescriptor(socketDescriptor);
  101. tcpClientSocketList.append(tcpClientSocket);
  102. }
  103. void Server::updateClients(QString msg,int length)
  104. {
  105. emit updateServer(msg,length);
  106. }
  107. void Server::send2Client(QString msg,int length)
  108. {
  109. for(int i=0;i<tcpClientSocketList.count();i++)
  110. {
  111. QTcpSocket *item=tcpClientSocketList.at(i);
  112. if(item->write(msg.toLatin1(), length)!=length)
  113. {
  114. continue ;
  115. }
  116. }
  117. }
  118. void Server::slotDisconnected(int descriptor)
  119. {
  120. for(int i=0;i<tcpClientSocketList.count();i++)
  121. {
  122. QTcpSocket *item=tcpClientSocketList.at(i);
  123. if(item->socketDescriptor ()==descriptor)
  124. {
  125. tcpClientSocketList.removeAt(i);
  126. return;
  127. }
  128. }
  129. return;
  130. }
  131. //tcpclientsocket.h
  132. #ifndef TCPCLIENTSOCKET_H
  133. #define TCPCLIENTSOCKET_H
  134. #include <QtGui>
  135. #include <QtNetwork>
  136. class TcpClientSocket : public QTcpSocket
  137. {
  138. Q_OBJECT
  139. public:
  140. TcpClientSocket( QObject *parent=0);
  141. ~TcpClientSocket();
  142. signals:
  143. void updateClients(QString,int);
  144. void disconnected(int);
  145. protected slots:
  146. void dataReceived();
  147. void slotDisconnected();
  148. };
  149. #endif
  150. //tcpclientsocket.cpp
  151. #include "rtu_server.h"
  152. TcpClientSocket::TcpClientSocket( QObject *parent)
  153. {
  154. connect(this, SIGNAL(readyRead()),this, SLOT(dataReceived()));
  155. connect(this, SIGNAL(disconnected()),this, SLOT(slotDisconnected()));
  156. }
  157. TcpClientSocket::~TcpClientSocket()
  158. {
  159. }
  160. void TcpClientSocket::dataReceived()
  161. {
  162. while (bytesAvailable()>0)
  163. {
  164. char buf[1024];
  165. int length=bytesAvailable();
  166. read(buf, length);
  167. QString msg=buf;
  168. emit updateClients(msg,length);
  169. }
  170. }
  171. void TcpClientSocket::slotDisconnected()
  172. {
  173. emit disconnected(this->socketDescriptor ());
  174. }

离线bingogo
只看该作者 1楼 发表于: 2009-08-13
加一些调试语句,看输出
qDebug( )  << "  ....  ";
快速回复
限100 字节
 
上一个 下一个