开发者

How to emit error for QLocalSocket

I am implementing 'Local fortune server' example (from Qt 4.7.3) as a service on Windows. What i want is that when someone paused the service, the local server should notify the error to the connected local socket (local fortune client). The error can be QLocalSocket::ServerNotFoundError. Now, How to generate this error from server example. Pleas开发者_运维问答e look at the following code where i want to generate this error.

void FortuneServer::incomingConnection(quintptr socketDescriptor)
{
 if (disabled) {
  **// here i want to emit QLocalSocket::ServerNotFoundError**
  return;
 }

 QString fortune = fortunes.at(qrand() % fortunes.size());
 FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
 thread->start();
}

void FortuneServer:: pause()
{
  disabled = true;
}


If you want your server to notify your clients I think you should create and send your own message like :

QString fortune = "Server down";

But this will occure when your server has incoming message.
or you can shutdown the server whith the QTcpServer::close() method

void FortuneServer:: pause()
{
    this.close();
}

your client app will lost its connection and you should be able to get the signal you want in it with this :

connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
             this, SLOT(displayError(QAbstractSocket::SocketError)));


You can't emit QLocalSocket::ServerNotFoundError, because it's not a signal. You should define your own signal and emit it instead (you can pass values in signals). You should also implement slot and connect signal to it. Read more about signals and slots.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜