0

i have a very simple question here, How can i send a string with

tcpserver->write(string);

I tried:

tcpserver->write("string") 

and it works, but if i want to input a string in there, i get a "no matching function to call to 'QtcpSocket::write(QString)'" error,

so i tried converting the string to "data" and then send it, but i got a ton of errors...

And my question is: How can i easly send a string thru my tcpserver?

(I should also mention, that i am very new to programming)

Ingmar05
  • 501
  • 2
  • 8
  • 25

4 Answers4

3

You need to convert string to QByteArray, for example:

tcpserver->write(string.toLocal8Bit());

1
tcpserver->write(string.toUtf8());
Jan Müller
  • 413
  • 3
  • 18
0

Try tcpserver->write((const char *)string.data(), string.length()*sizeof(QChar));

0

QTcpSocket has 3 overloads for write () function

qint64 write (const char *data);
qint64 write (const char *data, qint64 len);
qint64 write (const QByteArray &data);

So Convert QString to any of them. Just try

tcpserver->write (string.toLatin1 ());
ninja
  • 809
  • 5
  • 9