Results 1 to 6 of 6

Thread: Split a qstring and put the fragments in an array

  1. #1
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Split a qstring and put the fragments in an array

    Dear all,

    I implemented a simple chat server in order to receive a msg from a device. The msg is a string and i would like to tokenize it in order to put the pieces of it to my sql database. Could somebody send me a code example how this can be done?

    Thank you in advance!

    Qt Code:
    1. #include "simplechatserver.h"
    2. #include <QBuffer>
    3. #include <QByteArray>
    4. #include <QString>
    5. #include <QTcpSocket>
    6. #include <QStringList>
    7. #include <iostream>
    8.  
    9. SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
    10. {
    11. std::cout << "Message server started" << std::endl;
    12. connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
    13. }
    14.  
    15. SimpleChatServer::~SimpleChatServer()
    16. {
    17. }
    18.  
    19. void SimpleChatServer::addConnection()
    20. {
    21. std::cout <<"new incomming connection" << std::endl;
    22. QTcpSocket* connection = nextPendingConnection();
    23. connections.append(connection);
    24. QBuffer* buffer = new QBuffer(this);
    25. buffer->open(QIODevice::ReadWrite);
    26. buffers.insert(connection, buffer);
    27. connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
    28. connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
    29. }
    30.  
    31. void SimpleChatServer::removeConnection()
    32. {
    33. std::cout << "removing connection "<< std::endl;
    34. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    35. QBuffer* buffer = buffers.take(socket);
    36. buffer->close();
    37. buffer->deleteLater();
    38. connections.removeAll(socket);
    39. socket->deleteLater();
    40. }
    41.  
    42. void SimpleChatServer::receiveMessage()
    43. {
    44. std::cout << "receiving" <<std::endl;
    45. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    46. QBuffer* buffer = buffers.value(socket);
    47.  
    48. // missing some checks for returns values for the sake of simplicity
    49. qint64 bytes = buffer->write(socket->readAll());
    50. // go back as many bytes as we just wrote so that it can be read
    51. buffer->seek(buffer->pos() - bytes);
    52.  
    53.  
    54. // while(buffer->canReadLine())
    55. {
    56. line = buffer->readLine();
    57. QString str(line.data());
    58. std::cout << str.toStdString() << std::endl;
    59.  
    60.  
    61. QStringList list[] = {str.split(QRegExp(" "), QString::SkipEmptyParts)}; ????????????
    62. //std::cout<< list.<<std::endl; ???????????
    63.  
    64.  
    65.  
    66. emit emitMessage(str);
    67.  
    68.  
    69. }
    70. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Split a qstring and put the fragments in an array

    Just skip the brackets:
    Qt Code:
    1. QStringList list = str.split(" ", QString::SkipEmptyParts);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Split a qstring and put the fragments in an array

    Thank you very much!
    I would like to ask something more concerning the handling of this list. I did the following but is also not correct.
    I have the str= "15:12:00 milk"

    Qt Code:
    1. unsigned int hour, object;
    2. QString text;
    3.  
    4. QStringList list = str.split(" ", QString::SkipEmptyParts);
    5.  
    6.  
    7.  
    8. if (list.count() != 2)
    9.  
    10.  
    11.  
    12. text = list[1];
    13.  
    14. l = QStringList::split(':', list[0]);
    15.  
    16. hour = list[0].toUInt();
    17. object= list[1].toUInt();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Split a qstring and put the fragments in an array

    Let me ask a question: How long do you deal with C++? And to your problem: What's the difference between
    Qt Code:
    1. str.split(...);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. QStringList::split(...);
    To copy to clipboard, switch view to plain text mode 
    ?

  5. #5
    Join Date
    Feb 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Split a qstring and put the fragments in an array

    I am sorry but i am learning since one year now c++ and i am still doing basic mistakes.

    In the code instead of list = str.split(':', list[0]); i wrote l = QStringList::split(':', list[0]);.

    Could you please help me on how i should handle this?

  6. #6
    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: Split a qstring and put the fragments in an array

    Take a look at this code:

    Qt Code:
    1. l = QStringList::split(a,b);
    To copy to clipboard, switch view to plain text mode 

    Could you point me to the place in Qt docs where this method is documented?
    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.


Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 19:10
  2. Char array to QString
    By gab74 in forum Newbie
    Replies: 11
    Last Post: 13th December 2011, 15:54
  3. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 08:28
  4. Using an array and QString contains
    By TomJoad in forum Newbie
    Replies: 8
    Last Post: 12th April 2011, 05:31
  5. QString split()
    By ShaChris23 in forum Newbie
    Replies: 4
    Last Post: 3rd May 2007, 04:10

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.