Search code examples
c++qtc-stringsllvm-clangstring-conversion

qFatal argument: conversion of QString to const char* results in warning "format string is not a string literal"


Under Qt5.9 and clang++-6.0.0,

QString ret;
qFatal(ret.toLatin1().constData());

yields a warning "format string is not a string literal".

What's wrong, and what is the right way to accomplish the required conversion from QString to a C string?

PS: A closely related question is Converting QString to char*. Here, however, different solutions are possible thanks to the printf-like argument list of qFatal.


Solution

  • qFatal allows for the variadic ... argument known from printf. Thus

    qFatal("%s", ret.toLatin1().constData());
    

    and the warning is gone.