PDA

View Full Version : Split a qstring and put the fragments in an array



fantom
25th January 2012, 15:58
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!


#include "simplechatserver.h"
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QTcpSocket>
#include <QStringList>
#include <iostream>

SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
{
std::cout << "Message server started" << std::endl;
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}

SimpleChatServer::~SimpleChatServer()
{
}

void SimpleChatServer::addConnection()
{
std::cout <<"new incomming connection" << std::endl;
QTcpSocket* connection = nextPendingConnection();
connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}

void SimpleChatServer::removeConnection()
{
std::cout << "removing connection "<< std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}

void SimpleChatServer::receiveMessage()
{
std::cout << "receiving" <<std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);

// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);


// while(buffer->canReadLine())
{
line = buffer->readLine();
QString str(line.data());
std::cout << str.toStdString() << std::endl;


QStringList list[] = {str.split(QRegExp(" "), QString::SkipEmptyParts)}; ????????????
//std::cout<< list.<<std::endl; ???????????



emit emitMessage(str);


}
}

Lykurg
25th January 2012, 16:04
Just skip the brackets:
QStringList list = str.split(" ", QString::SkipEmptyParts);

fantom
25th January 2012, 16:22
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"



unsigned int hour, object;
QString text;

QStringList list = str.split(" ", QString::SkipEmptyParts);



if (list.count() != 2)



text = list[1];

l = QStringList::split(':', list[0]);

hour = list[0].toUInt();
object= list[1].toUInt();

Lykurg
25th January 2012, 16:52
Let me ask a question: How long do you deal with C++? And to your problem: What's the difference between
str.split(...); and
QStringList::split(...);?

fantom
26th January 2012, 13:26
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?

wysota
26th January 2012, 14:04
Take a look at this code:


l = QStringList::split(a,b);

Could you point me to the place in Qt docs where this method is documented?