问题
In my Qt5 application, I am receiving a log message at run time from Qt itself that looks like this:
QObject::connect: Cannot queue arguments of type 'QHostAddress'
I know how to fix the cause of this message using Q_DECLARE_METATYPE(QHostAddress)
macro or qRegisterMetaType<QHostAddress>("QHostAddress")
class, that is not what this question is about.
What I want to know is, how can I pin-point the exact location of the call in my code that triggered this message?
Since the message does not come with a file/line number and since I seem to be unable to set debugger break-points inside Qt5 itself I can't find out what causes this message.
回答1:
I create a message handler to catch the messages:
void myMessageHandler(QtMsgType type, const QMessageLogContext & logContext, const QString & msg)
{
Q_UNUSED(logContext);
switch (type) {
case QtDebugMsg:
std::cerr << qPrintable(QString("qDebug: %1").arg(msg)) << std::endl;
break;
case QtWarningMsg:
std::cerr << qPrintable(msg) << std::endl;
break;
case QtCriticalMsg:
std::cerr << qPrintable(QString("qCritical: %1").arg(msg)) << std::endl;
break;
case QtFatalMsg:
std::cerr << qPrintable( QString("qFatal: %1").arg(msg) ) << std::end;
abort();
}
}
Then in main install the message handler:
int main()
{
qInstallMessageHandler(myMessageHandler);
// rest of main
}
Then I set a breakpoint on the warning case. When the breakpoint is hit I walk up the callstack to the line of code that triggered the warning. On some of my applications I have put an assert(false) instead so I don't forget to set the breakpoint.
来源:https://stackoverflow.com/questions/47624731/how-can-i-pin-point-the-location-of-the-call-in-my-code-that-triggered-qobject