我尽量避免在 QT 程序中混合QString
和输入。char*
我有一个对话函数,它返回内部数据的指针QString
,但我得到了非常奇怪的结果。这段代码有什么问题?
编译器和环境 gcc (Debian 4.9.2-10) 4.9.2 通过 qmake 的标志:QMAKE_CXXFLAGS += -std=c++11
代码片段:
#include <QCoreApplication>
#include <iostream>
#define PCH(m) ( m.toAscii().data() )
// get the pointer short scripted
const char* CH(QString msg) {
const char* res = msg.toAscii().data();
return res;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const QString qformat = "%s does not work!";
const QString qvalue = "embedding";
// Works not
qDebug(CH(qformat), CH(qvalue));
// Works
qDebug(qformat.toAscii().data(), qvalue.toAscii().data());
// Works too but macro
qDebug(PCH(qformat), PCH(qvalue));
return app.exec();
}
结果
%s does not work! does not work!
embedding does not work!
embedding does not work!