PDA

View Full Version : binding to qInstallMessageHandler



TorAn
20th December 2013, 13:10
I want to use a class member as a handler for Qt output. Following code fails to compile; I'll appreciate a helping hand



void test::debugOut(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{

}

void test::onConnect()
{
auto fp = std::bind (&test::debugOut, *this, std::placeholders::_1);
qInstallMessageHandler(fp);
}

anda_skoa
21st December 2013, 10:14
What about using a singleton and make the debugOut function a static that accesses the singleton instance?

Cheers,
_

TorAn
21st December 2013, 12:50
We live in the age of C++11 and new std, man. :)
Following should compile, but does not and I can't figure out why.



auto fp = std::bind (&test::debugOut, *this, std::placeholders::_1);
qInstallMessageHandler(fp);

anda_skoa
21st December 2013, 13:52
Well, you are not giving us any error message, so trying to help with your actual problem is not an option.

My guess is that the compiler doesn't like your attempt to bind *this as the QMsgType argument of the given function.

I know it isn't C++11, man, but from a way older standard version, but maybe std::mem_fun() is what you are looking for?

Cheers,
_

TorAn
21st December 2013, 16:51
First of all - thank you for your replies, I mean it!

The error (VS2012, I'll upgrade to 13 tonight to see if it helps) is:

error C2664: 'qInstallMessageHandler' : cannot convert parameter 1 from 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V 5_t,<unnamed-symbol>>' to 'QtMessageHandler'
with
[
_Forced=true,
_Ret=void,
_Fun=std::_Pmf_wrap<void (__thiscall test::* )(QtMsgType,const QMessageLogContext &,const QString &),void,test,QtMsgType,const QMessageLogContext &,const QString &,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
_V0_t=test &,
_V1_t=std::_Ph<1> &,
_V2_t=std::_Nil,
_V3_t=std::_Nil,
_V4_t=std::_Nil,
_V5_t=std::_Nil,
<unnamed-symbol>=std::_Nil
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called