QT5.14: 打开文件出错warning: format '%s' expects argument of type 'char*'

发布时间 2023-10-05 22:11:33作者: samrv

错误提示信息:

D:\Demo\QT5.14\CH5\CH501\imgprocessor.cpp:158: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'QChar*' [-Wformat=]
printf("file Name:%s\n",filename.data());

原函数代码:

void ImgProcessor::loadFile(QString filename)
{
    printf("file Name:%s\n", filename.data());
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QTextStream textStream(&file);
        while(!textStream.atEnd()){
            showWidget->text->append(textStream.readLine());
            printf("read line\n");
        }
        printf("end\n");
    }
}

  

修改后,没有提示:

void ImgProcessor::loadFile(QString filename)
{
    printf("file Name:%s\n", (char*)filename.data());
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QTextStream textStream(&file);
        while(!textStream.atEnd()){
            showWidget->text->append(textStream.readLine());
            printf("read line\n");
        }
        printf("end\n");
    }
}

  主要修改句:

   printf("file Name:%s\n", (char*)filename.data());