QT源码中有中文乱码的情况

一、qt中的文件一般都是utf8格式,所以需要使用QString承载中文字符串常量。

  比如:

  QString image_file_name=“中文”;

  qDebug() << image_file_name;
  这样不会乱码

二、qt中的QString转换为std::string时,需要转换到本地编码,使用std::cout或cv::imread的时候才能正常使用。转换方式是std::string str=image_file_name.toLocal8Bit().toStdString(),这样就能在std::cout或cv::imread中使用str进行操作

  比如

  std::string str=image_file_name.toLocal8Bit().toStdString();

  std::cout << str <<std::endl;

  cv::Mat image=cv::imread(str);

  这样不会乱码且正常使用

posted on 2025-08-26 17:59  哈哈木  阅读(39)  评论(0)    收藏  举报