PDA

View Full Version : QStringList::contains() not working



nmuntz
21st May 2008, 14:07
Hello,
I'm trying to find a string inside a QStringList which is in a loop, but it doesn't seem to be working. I'm not sure whether I'm doing it right or not.
I tried debugging it for hours with no luck. It might be the way I'm parsing the data coming from the QTcpSocket ?
Am i doing it wrong?

The code is below:



void Core::connected()
QTextStream in(tcpSocket);
QString data;
do
{
data = in.readLine();
QStringList words = data.split(" ");

if ( words.count() > 2 )
{
// at this point, words should be something like = ("nmuntz@localhost", "MSG", ":VERSION");
if ( words.contains(":VERSION", Qt::CaseInsensitive) )
qDebug() << "words contains the word version!!!";
}

while ( !in.atEnd() ) ;
}
}


Thanks a lot in advance!
Greetings.

nouknouk
21st May 2008, 14:30
Hi,

2 maybe 3 ideas:

- add a qDebug() statement too in case you don't find the searched string: maybe the debug line is not displayed because you forgot something like 'qt+= console' in your .pro file.

- display (in a qDebug) the content of the QStringList, thus, you'll ensure that what you are searching for is REALLY inside the list.

On my side, i bet you don't have what expected in your string list.

nmuntz
21st May 2008, 14:40
Hi,
First, thanks for the quick reply !

Hi,

2 maybe 3 ideas:

- add a qDebug() statement too in case you don't find the searched string: maybe the debug line is not displayed because you forgot something like 'qt+= console' in your .pro file.

Debugging is correctly working in my program, I have other qDebug() lines and they are correctly working...



- display (in a qDebug) the content of the QStringList, thus, you'll ensure that what you are searching for is REALLY inside the list.

On my side, i bet you don't have what expected in your string list.
I already tried displaying the content of the QStringList, but my program ends unexpectedly with a

ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/QtCore/qlist.h, line 399

Greetings,

mazurekwrc
21st May 2008, 14:49
use

qDebug()<<words;
to see what exactly contain your QStringList

nmuntz
21st May 2008, 15:01
use

qDebug()<<words;
to see what exactly contain your QStringList

here is the output of the last 2 lines:


words is: (":nmuntz@localhost", "MODE", "nmuntz", ":+iw")
words is: (":nmuntz@localhost", "MSG", "nmuntz", ":VERSION")

so my code, when the last line appears, should be doing something......

mazurekwrc
21st May 2008, 15:20
could you copy your original code from your file, becouse I see in that code which you insert few syntax mistakes so I think you don't use copy/paste, maybe there are some other difference and we can't see them

nmuntz
21st May 2008, 15:45
could you copy your original code from your file, becouse I see in that code which you insert few syntax mistakes so I think you don't use copy/paste, maybe there are some other difference and we can't see them

the code I posted is exactly the code I'm using... I just tried recoding the function with a copy paste of the code I pasted here and it's not working...
This is really strange, because it should be working...

Im pretty sure it has to do with the way im handling the QTextStream from the tcpSocket... there's something about that loop that doesn't seem right...

mazurekwrc
21st May 2008, 15:51
the code I posted is exactly the code I'm using... I just tried recoding the function with a copy paste of the code I pasted here and it's not working...
This is really strange, because it should be working...

becouse this code have syntax mistakes for example


do
{
while( !in.atEnd() ) ;
}

patrik08
21st May 2008, 16:05
Yes i think the same ...

here so you can grab line....




if (m_Socket->waitForReadyRead(Timeout)) {
qDebug() << "### connect ok port " << url.port();
QTextStream *t = new QTextStream( m_Socket );
t->setCodec(QTextCodec::codecForMib(106));
int loops = -1;
while (!t->atEnd()) {
loops++;
response = t->readLine();
qDebug() << loops << " line " << response;
}
emit result(url.port(),runtime(st),true);
quit();
} else {



Have a look on portscanner sample..

http://www.qtcentre.org/forum/f-qt-programming-2/t-qsqldatabase-mysql-timeout-versus-portscanner-13733.html

nmuntz
21st May 2008, 16:30
patrick08:

Ok, so I used your code:


void QtBotCore::connected()
{
QTextStream *t = new QTextStream( tcpSocket );
QString response;
int loops = -1;
while ( !t->atEnd() )
{
loops++;
response = t->readLine();
qDebug() << loops << " line " << response;

if ( response.contains(":VERSION") )
qDebug() << "IT WORKED !";
}
}


And the output is something like this:


0 line "127.0.0.1 372 nmuntz :- ____ _____ _______ ____ ____ _"
1 line ":127.0.0.1 372 nmuntz :- | _ \ / _ \ \ / / ____| _ \/ ___| ___| |"
2 line ":127.0.0.1 372 nmuntz :- Welcome to Server"
3 line ":127.0.0.1 372 nmuntz :- "
4 line ":127.0.0.1 376 nmuntz :End of /MOTD command."
5 line ":nmuntz MODE nmuntz :+iw"
6 line ":Global!services@powers.cl NOTICE nmuntz :[Logon News - May 03 2006] PWN!"
7 line ":nmuntz@pool.verizon.net PRIVMSG nmuntz :VERSION"