PDA

View Full Version : How to remove this warning: converting 'false' to pointer type for argument QObject*



ricardodovalle
7th April 2014, 21:44
This warning only show when i attributed the false value.

At windows:
C:\Users\ricardo\Documents\Thorx\workspace\thx6\2\ src\app\main.cpp:88: warning: converting 'false' to pointer type for argument 2 of 'void QQmlContext::setContextProperty(const QString&, QObject*)' [-Wconversion-null]
engine.rootContext()->setContextProperty("QT_DEBUG", false);

At OSX:
/Users/ricardo/Thorx/workspace/thx6/2/src/app/main.cpp:88: warning: initialization of pointer of type 'QObject *' to null from a constant boolean expression [-Wbool-conversion]
engine.rootContext()->setContextProperty("QT_DEBUG", false);

Source code


QQmlApplicationEngine engine;
#ifdef QT_DEBUG
engine.rootContext()->setContextProperty("QT_DEBUG", true);
#else
engine.rootContext()->setContextProperty("QT_DEBUG", false);
#endif


Thanks.

ChrisW67
7th April 2014, 23:44
The function is overloaded:


void QDeclarativeContext::setContextProperty ( const QString & name, QObject * value )
void QDeclarativeContext::setContextProperty ( const QString & name, const QVariant & value )

You give the compiler a second argument of bool. Since neither overload takes a bool argument explicitly the compiler tries to find a match by casting. In both cases it chooses the QObject* version and issues the warning because using a bool to initialise a pointer is unusual.

Do this to avoid the warning by forcing the QVariant version:


QQmlApplicationEngine engine;
#ifdef QT_DEBUG
engine.rootContext()->setContextProperty("QT_DEBUG", QVariant(true));
#else
engine.rootContext()->setContextProperty("QT_DEBUG", QVariant(false));
#endif