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!