Results 1 to 3 of 3

Thread: TCP Client issue

  1. #1
    Join Date
    May 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default TCP Client issue

    Hello,
    I'd like to convert a char array to a string so I can show it in a status label or a text edit box:
    Example:
    Qt Code:
    1. char temp[4] = {0x01, 0x03, 0x03, 0x04};
    2. ....
    3. statusLabel->setText(QString::fromAscii(temp, 4));
    To copy to clipboard, switch view to plain text mode 
    The status label should show ideally 0x01 0x02 0x03 0x04.

    I'm trying to do this in the fortune client example from Qt Creator.

    Qt Code:
    1. void Client::readFortune()
    2. {
    3. QDataStream in(tcpSocket);
    4. in.setVersion(QDataStream::Qt_4_3);
    5.  
    6. char temp[30];
    7. int flag = 0;
    8. char temp2[4] = {0x01, 0x02, 0x03, 0x04};
    9.  
    10. if (blockSize == 0) {
    11. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    12. return;
    13.  
    14. in >> blockSize;
    15. }
    16.  
    17. flag = tcpSocket->readLine(temp, 31);// i'm trying to read the receive buffer here and show it below
    18. if(flag |= -1){
    19. messageLineEdit->setText(QString::number(blockSize)); // I can see the block size, 1537
    20. statusLabel->setText(QString::fromAscii(temp2, 4));
    21. }
    22.  
    23. if (tcpSocket->bytesAvailable() < blockSize)
    24. return;
    25.  
    26. QString nextFortune;
    27. in >> nextFortune;
    28.  
    29. if (nextFortune == currentFortune) {
    30. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    31. return;
    32. }
    33.  
    34. currentFortune = nextFortune;
    35.  
    36. statusLabel->setText(currentFortune);
    37.  
    38. getFortuneButton->setEnabled(true);
    39. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Client issue

    Quote Originally Posted by LDS View Post
    Hello,
    I'd like to convert a char array to a string so I can show it in a status label or a text edit box:
    Example:
    Qt Code:
    1. char temp[4] = {0x01, 0x03, 0x03, 0x04};
    2. ....
    3. statusLabel->setText(QString::fromAscii(temp, 4));
    To copy to clipboard, switch view to plain text mode 
    The status label should show ideally 0x01 0x02 0x03 0x04.
    0x01-0x04 are unprintable characters. What you want is probably this:
    Qt Code:
    1. char temp[4] = {0x01, 0x03, 0x03, 0x04};
    2. for(int i=0;i<4;i++) {
    3. hex << QString("0x%1").arg((int)temp[i], 2, 16, QLatin1Char('0'));
    4. }
    5. statusLabel->setText(hex.join(" "));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    LDS (6th May 2010)

  4. #3
    Join Date
    May 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TCP Client issue

    Thank you for your quick answer.
    I know I couldn't possibly print it directly, I already developed de server that sends the data to the client and I had to do the same thing but in C.
    Anyway, it worked perfectly!


    Thank you for your help,
    LDS

    Thread solved.

Similar Threads

  1. DBus Client
    By junix in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 13:53
  2. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 10:10
  3. WebIssues Client 0.9.5
    By mimec in forum Qt-based Software
    Replies: 0
    Last Post: 17th August 2009, 12:01
  4. client-server how?
    By nongentesimus in forum Newbie
    Replies: 6
    Last Post: 28th November 2006, 09:25
  5. OPC client with Qt
    By hafwil in forum Qt Programming
    Replies: 0
    Last Post: 18th May 2006, 18:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.