PDA

View Full Version : qDebug - how to avoid CRCL (newline character)?



johnyjj2
11th January 2011, 15:09
Hello!

I'd like to output vector to the qDebug in QT Creator. Unfortunately every number is in separated line. How can I avoid it?

If I execute similar code and output vector of vectors, the result looks as follows:
(1,2,3)
(2,4,6)

But if I do similar thing, i.e. only vector, I receive every number of the vector in separate line. I don't want qDebug to put characters of new line at the end of every execution. In order to avoid this, I have tried several different things but I haven't succeeded. I have either every number in separated line or build error (the third option is just no output in qDebug).

The code looks as follows:


//***** TESTING: SHOW DISTANCES AND WEIGHTS *****

//the version below works but everything is in separated line (with only zero digit in every of them)
QList<int>::iterator it2 = distances.begin();
qDebug() << "distances: " << *it2;
//the line below should do the same what the line above (without new line characters) but it does not work
//fprintf (stderr, "%s ", *it2);
//the line above was found here http://lists.trolltech.com/qt-interest/2005-11/thread00923-0.html

//the version below don't work; it was expected to write single line (0,0,0,...)
//QList< QList<int> >::iterator it2 = distances.begin();
//for( ; it2 != distances.end(); ++it2 )
// qDebug() << *it2 << " ";

//***********************************************

Regards!

high_flyer
11th January 2011, 15:21
I guess the easiest way (but far not the best or the only one) is to append first all the strings in to one string, and output only that string with one qDebug() call.

norobro
11th January 2011, 15:35
How about:

qDebug() << distances;

johnyjj2
16th January 2011, 09:33
Thanks!

1. What if I have got several different things which I'd like to output to qDebug? Those are in different parts of the code but are short enough to be stored in one single line. In other words, is there any way to write "qDebug << backspace_character" in order to delete CR/CL characters of new line?

2. And the second question, more connected with object-oriented programming. I've got files created automatically by QT Creator: main.cpp, mainwindow.h, mainwindow.cpp. There are also files which I have added: mydata.h, mydata.cpp. In the file mydata.cpp I have function breadth() for breadth search algorithm. I use qDebug() inside this breadth() function. However I use it many times in different places of the code. What if I wanted to use one function, e.g. showDebugInfo(variables which are declared inside the breadth function to be given by reference) and call it inside breadth() function? I mean, where should I declare this showDebugInfo(...) function? (Because it would be like function called from inside the function which is method of the class - it makes me confused). And how to give those other variables as reference to that function? I've got such a code at this moment:


qDebug() << "Value of n = " << n;
qDebug() << "distances: " << distances;
//...

where distances is QList<int> distances; and there are more lists like this in my qDebug part of the code.

Regards!