我还没有找到类似的问题,所以这里是:
我正在跨两个应用程序将 QString 从 QLocalSocket 发送到 QLocalServer 。接收(QLocalServer)应用程序确实收到了消息,但似乎编码完全错误。
如果我从 QLocalSocket(客户端)发送一个 QString = "x",我会在 QLocalServer 中得到一个外国(中文?)符号。我的代码实际上是从诺基亚开发者网站复制而来的
如果我通过 QDebug 打印出消息,我会得到“??”。如果我在消息框中触发它,则会打印中文字符。我尝试将收到的消息重新编码为 UTF-8、Latin1 等,但没有成功。
代码如下:
//Client
int main(int argc, char *argv[])
{
QLocalSocket * m_socket = new QLocalSocket();
m_socket->connectToServer("SomeServer");
if(m_socket->waitForConnected(1000))
{
//send a message to the server
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << "x";
out.device()->seek(0);
m_socket->write(block);
m_socket->flush();
QMessageBox box;
box.setText("mesage has been sent");
box.exec();
...
}
//Server - this is within a QMainWindow
void MainWindow::messageReceived()
{
QLocalSocket *clientConnection = m_pServer->nextPendingConnection();
while (clientConnection->bytesAvailable() < (int)sizeof(quint32))
clientConnection->waitForReadyRead();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_7);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
QMessageBox box;
box.setText(QString(message));
box.exec();
}
非常感谢任何帮助。