简体   繁体   English

打印Qt变量

[英]Printing Qt variables

I'm programming in Qt, but I'm more accustomed to PHP. 我在Qt编程,但我更习惯于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? 因此,考虑到这一点,我如何“回显”或“打印”QStringList或QString的内容以确保内容符合预期?

I'm building a GUI application. 我正在构建一个GUI应用程序。 Is there anyway to print the contents? 无论如何打印内容?

Obviously in PHP, you can print_r on an array, is there anything similar for a QStringList? 显然在PHP中,你可以在一个数组上使用print_r,QStringList有什么类似的东西吗? And echo a variable, again, anything similar to QString? 并且回显一个变量,类似于QString的任何东西?

I can provide code if needs be. 如果需要,我可以提供代码。

Thanks. 谢谢。

main.cpp 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 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. 如果您需要删除qDebug生成的嘈杂括号和双引号,您可以自由地使用QTextStream进行自定义打印,或者只需使用自定义打印回退到标准cout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM