4

I'm programming in Qt, but I'm more accustomed to PHP.

So with that in mind, how do I 'echo' or 'print' out the contents of a QStringList or QString to ensure the contents are as expected?

I'm building a GUI application. Is there anyway to print the contents?

Obviously in PHP, you can print_r on an array, is there anything similar for a QStringList? And echo a variable, again, anything similar to QString?

I can provide code if needs be.

Thanks.

0

1 Answer 1

8

main.cpp

#include <QStringList>
#include <QDebug>

int main()
{
    QStringList myStringList{"Foo", "Bar", "Baz"};
    qDebug() << myStringList;
    QString myString = "Hello World!";
    qDebug() << myString;
    return 0;
}

main.pro

TEMPLATE = app
TARGET = print-qstringlist
QT = core
CONFIG += c++11
SOURCES += main.cpp

Build and Run

qmake && (n)make

Output

("Foo", "Bar", "Baz")
"Hello World!"

If you need to drop the noisy brackets and double quotes generated by qDebug, you are free to either use QTextStream with custom printing or simply fall back to the standard cout with custom printing.

Sign up to request clarification or add additional context in comments.

3 Comments

I included the QDebug and used the code you provided in the cpp. Perfect. I didn't need to alter the pro file...what are you adding in there that I need?
It worked without adding the pro file entries, so I was wondering what the TARGET = print-qstringlist is doing? Awesome answer thanks!
@Luke: yeah, if you do not specify the TARGET variable, the generated executable will be called based on the parent folder name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.