-
UID:81493
-
- 注册时间2009-06-30
- 最后登录2023-11-18
- 在线时间340小时
-
- 发帖6
- 搜Ta的帖子
- 精华0
- 金钱60
- 威望16
- 贡献值0
- 好评度6
-
访问TA的空间加好友用道具
|
小弟最近由書本範例改自己想要的聊天室程式 遇到一個問題,就是使用emit去呼叫,但都沒作用 由rtu_server.cpp中的emit send2Client(txtSend,length); 但都沒反應~ 請各位高手幫我看看看哪裡出問題~感謝 ! - //rtu_server.h
- #ifndef RTU_SERVER_H
- #define RTU_SERVER_H
- #include <QtGui/QWidget>
- #include "ui_rtu_server.h"
- #include "server.h"
- class RTU_Server : public QWidget
- {
- Q_OBJECT
- public:
- RTU_Server(QWidget *parent = 0);
- ~RTU_Server();
- public:
- QListWidget *ListWidgetContent;
- QLabel* LabelPort;
- QLineEdit* LineEditPort;
- QPushButton* PushButtonCreate;
- int port;
- Server *server;
- QString txtSend;
- signals:
- void send2Client(QString,int);
- public slots:
- void slotCreateServer();
- void updateServer(QString,int);
- void slotSend2Client();
- private:
- Ui::RTU_ServerClass ui;
- };
- #endif
- //rtu_server.cpp
- #include "rtu_server.h"
- #include "server.h"
- RTU_Server::RTU_Server(QWidget *parent)
- : QWidget(parent)
- {
- ui.setupUi(this);
- connect(ui.PushButtonCreate,SIGNAL(clicked()),this,SLOT(slotCreateServer()));
- port = 8010;
- ui.LineEditPort->setText(QString::number(port));
- ui.PushButtonSend->setEnabled(false);
- connect(ui.PushButtonSend,SIGNAL(clicked()),this,SLOT(slotSend2Client()));
- }
- RTU_Server::~RTU_Server()
- {
- }
- void RTU_Server::slotCreateServer()
- {
- server = new Server(this,port);
- connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int)));
- ui.PushButtonCreate->setEnabled(false);
- ui.PushButtonSend->setEnabled(true);
- }
- void RTU_Server::updateServer(QString msg,int length)
- {
- ui.ListWidgetContent->addItem (msg.left(length) );
- }
- void RTU_Server::slotSend2Client()
- {
- txtSend = "Server:" + ui.LineEditSend->text();
- int length = 0;
- emit send2Client(txtSend,length);
- ui.LineEditSend->clear();
- ui.ListWidgetContent->addItem (txtSend);
- }
- //server.h
- #ifndef SERVER_H
- #define SERVER_H
- #include <QtNetwork>
- #include "tcpclientsocket.h"
- class Server : public QTcpServer
- {
- Q_OBJECT
- public:
- Server(QObject *parent = 0,int port=0);
- QList<TcpClientSocket*> tcpClientSocketList;
- signals:
- void updateServer(QString,int);
- public slots:
- void updateClients(QString,int);
- void slotDisconnected(int);
- void send2Client(QString,int);
- protected:
- void incomingConnection(int socketDescriptor);
- };
- #endif
- //server.cpp
- #include <QtNetwork>
- #include "server.h"
- Server::Server(QObject *parent,int port)
- : QTcpServer(parent)
- {
- listen(QHostAddress::Any,port);
- }
- void Server::incomingConnection(int socketDescriptor)
- {
- TcpClientSocket *tcpClientSocket = new TcpClientSocket(this);
- connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int)));
- connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));
- tcpClientSocket->setSocketDescriptor(socketDescriptor);
- tcpClientSocketList.append(tcpClientSocket);
- }
- void Server::updateClients(QString msg,int length)
- {
- emit updateServer(msg,length);
- }
- void Server::send2Client(QString msg,int length)
- {
- for(int i=0;i<tcpClientSocketList.count();i++)
- {
- QTcpSocket *item=tcpClientSocketList.at(i);
- if(item->write(msg.toLatin1(), length)!=length)
- {
- continue ;
- }
- }
- }
- void Server::slotDisconnected(int descriptor)
- {
- for(int i=0;i<tcpClientSocketList.count();i++)
- {
- QTcpSocket *item=tcpClientSocketList.at(i);
- if(item->socketDescriptor ()==descriptor)
- {
- tcpClientSocketList.removeAt(i);
- return;
- }
- }
- return;
- }
- //tcpclientsocket.h
- #ifndef TCPCLIENTSOCKET_H
- #define TCPCLIENTSOCKET_H
- #include <QtGui>
- #include <QtNetwork>
- class TcpClientSocket : public QTcpSocket
- {
- Q_OBJECT
- public:
- TcpClientSocket( QObject *parent=0);
- ~TcpClientSocket();
- signals:
- void updateClients(QString,int);
- void disconnected(int);
- protected slots:
- void dataReceived();
- void slotDisconnected();
- };
- #endif
- //tcpclientsocket.cpp
- #include "rtu_server.h"
- TcpClientSocket::TcpClientSocket( QObject *parent)
- {
- connect(this, SIGNAL(readyRead()),this, SLOT(dataReceived()));
- connect(this, SIGNAL(disconnected()),this, SLOT(slotDisconnected()));
- }
- TcpClientSocket::~TcpClientSocket()
- {
- }
- void TcpClientSocket::dataReceived()
- {
- while (bytesAvailable()>0)
- {
- char buf[1024];
- int length=bytesAvailable();
- read(buf, length);
- QString msg=buf;
- emit updateClients(msg,length);
- }
- }
- void TcpClientSocket::slotDisconnected()
- {
- emit disconnected(this->socketDescriptor ());
- }
|