windows Qstring 格式化字符串

Windows c++格式化字符串是个不省事的活,还想保证和mac平台保存通用,跨平台特性也得支持,调研一番,貌似只有Qstring符合了,特此记录一下

  • arg函数只支持字符串
  • 参数最多九个
      using namespace std;
      QString network = QString("{\"network_type\": \"%1\", \"rtt\": %2, \"loss_rate\": %3, \"upload_use_bw\": %4, \"download_use_bw\": %5 }").arg(
        ui_network.network_type.c_str(), to_string(ui_network.rtt).c_str(), to_string(ui_network.loss_rate).c_str(), to_string(ui_network.upload_use_bw).c_str(), to_string(ui_network.download_use_bw).c_str());
      QString system = QString("{\"basic_frequency\": \"%1\", \"cores\": %2, \"total_memory\": \"%3\", \"current_cpu\": %4, \"current_memory\": %5, \"other_cpu\": %6, \"other_memory\": %7 }").arg(
        to_string(ui_system.basic_frequency).c_str(), to_string(ui_system.cores).c_str(), to_string(ui_system.total_memory).c_str(), to_string(ui_system.current_cpu).c_str(), 
        to_string(ui_system.current_memory).c_str(), to_string(ui_system.other_cpu).c_str(), to_string(ui_system.other_memory).c_str());
      QString audio = QString("{\"upload\": {\"bitrate\": %1 }, \"download\": {\"bitrate\": %2 } }").arg(
        to_string(ui_audio.upload.bitrate).c_str(), to_string(ui_audio.download.bitrate).c_str());
      QString camera = QString(" {\"upload\": {\"resolution\": \"%1\", \"fps\": %2, \"bitrate\": %3 }, \"download\": {\"resolution\": \"%4\", \"fps\": %5, \"bitrate\": %6 } }").arg(
        ui_camera.upload.resolution.c_str(), to_string(ui_camera.upload.fps).c_str(), to_string(ui_camera.upload.bitrate).c_str(), 
        ui_camera.download.resolution.c_str(), to_string(ui_camera.download.fps).c_str(), to_string(ui_camera.download.bitrate).c_str());
      QString screen = QString(" {\"upload\": {\"resolution\": \"%1\", \"fps\": %2, \"bitrate\": %3 }, \"download\": {\"resolution\": \"%4\", \"fps\": %5, \"bitrate\": %6 } }").arg(
        ui_screen.upload.resolution.c_str(), to_string(ui_screen.upload.fps).c_str(), to_string(ui_screen.upload.bitrate).c_str(),
        ui_screen.download.resolution.c_str(), to_string(ui_screen.download.fps).c_str(), to_string(ui_screen.download.bitrate).c_str());
      QString str = QString("{ \"ui_network\": %1, \"ui_system\": %2, \"ui_audio\": %3, \"ui_camera\":%4, \"ui_screen\": %5}").arg(
        network.toStdString().c_str(), system.toStdString().c_str(), audio.toStdString().c_str(), camera.toStdString().c_str(), screen.toStdString().c_str());
      return str.toStdString();

 

posted @ 2021-11-23 11:11  2BiTT  阅读(257)  评论(0编辑  收藏  举报