如何将std :: string_view转换为QStringView

如何解决如何将std :: string_view转换为QStringView

我尝试了以下转换,但是所有转换都给我一个no matching constructor for initialization of 'QStringView'错误(注释反映了我尝试调用的构造函数):

string someString = "hello world";
string_view strView( someString );

// QStringView(const Char (&)[N] string = N) or
// QStringView(const Char *str,qsizetype len)
QStringView qStrView1(strView.data(),strView.size());

// QStringView(const Char *first,const Char *last)
QStringView qStrView2(strView.data(),strView.data() + strView.size());

// QStringView(const Char *first,const Char *last)
QStringView qStrView3(strView.begin(),strView.end());
QStringView qStrView3a(strView.cbegin(),strView.cend());

// QStringView(const Char *str)
QStringView qStrView4(strView.data());

(我认为第一或第二次转换可能会起作用,只是出于失望而尝试了第三和第四次转换。)

  • 有人可以指出正确的转换吗?我想念什么吗?
  • 还是我需要将所有std::string复制为QString,并通过重用开始/结束位置来从其中创建QStringView
  • (除此之外,我现在还没有弄清楚如何将QStringView插入QTableWidgetItem中。对于这个问题,我也将不胜感激。)

上下文

我以std::string的形式读取文件内容,并根据分隔符将其分为几行。为了有效地执行此操作,我生成了std::string_viewstd::vector来存储字段。现在,我想在Qt GUI中可视化字符串(实际上是针对QTableWidgetItem的),并认为QStringView可能对我的GUI部分很有用。我想使BusinessLogic独立于GUI,因此避免在其中包含Qt库。

设置

  • Qt 5.15.0
    CONFIG += c++17
    
  • MSVC2019 amd64
  • Windows 10

错误

(我删除了note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument,but 2 were providednote: candidate constructor template not viable: requires single argument 'str',but 2 arguments were provided等)

qStrView1

qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view::size_type' (aka 'unsigned long long')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]

qStrView2

qstringview.h:173:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:178:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleCharType<char>::value' was not satisfied [with Char = char]
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]

qStrView3

qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char,std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:178:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char,std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]

qStrView3a

qstringview.h:173:22: note: candidate template ignored: could not match 'const Char *' against 'std::basic_string_view<char,std::char_traits<char> >::const_iterator' (aka '_String_view_iterator<std::char_traits<char> >')
qstringview.h:191:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<std::_String_view_iterator<std::char_traits<char> > >::value' was not satisfied [with Array = std::_String_view_iterator<std::char_traits<char> >]

qStrView4

qstringview.h:103:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::basic_string_view<char,std::char_traits<char> >::const_pointer' (aka 'const char *') to 'const QStringView' for 1st argument
qstringview.h:103:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::basic_string_view<char,std::char_traits<char> >::const_pointer' (aka 'const char *') to 'QStringView' for 1st argument
qstringview.h:169:22: note: candidate constructor not viable: no known conversion from 'std::basic_string_view<char,std::char_traits<char> >::const_pointer' (aka 'const char *') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
qstringview.h:196:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleArray<const char *>::value' was not satisfied [with Array = const char *]
qstringview.h:200:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatiblePointer<const char *>::value' was not satisfied [with Pointer = const char *]
qstringview.h:209:5: note: candidate template ignored: requirement 'std::is_same<const char *,QString>::value || std::is_same<const char *,QStringRef>::value' was not satisfied [with String = const char *]
qstringview.h:214:22: note: candidate template ignored: requirement 'QtPrivate::IsCompatibleStdBasicString<const char *>::value' was not satisfied [with StdBasicString = const char *]

解决方法

感谢您将我引向std::wstring。我花了一些时间来了解wchar_twstringwcoutwifstream等事物,但是终于有了以下代码:

注意:.pro文件中仍需要CONFIG += 17,且包含以下内容

#include <QStringView>
#include <string_view>
#include <string>

// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-";   //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv2a(wview.data(),wview.size());
QStringView qstrv2b(wview.data(),wview.data() + wview.size());

以上任何一个构造函数都可以正常工作。

在QLabel中使用QStringView的示例

// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);

// create wstring and convert to QStringView
wstring somewstring = L"abc -ä-ö-ü-ß-";   //!< example string
wstring_view wview(somewstring);
// either work:
QStringView qstrv1(wview.data(),wview.size());

// convert to label
QLabel* otherLabel = new QLabel(qstrv1.toLocal8Bit());
mainLayout->addWidget(otherLabel);

从文件到QLabel的示例

// additinonal standard libararies:
#include <fstream>
#include <sstream>

// setup minimum GUI
QWidget* mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainWidget->setLayout(mainLayout);

// read file with UTF-8 characters (to a std::string)
std::string tempFileContent;
ifstream file("./infile.txt",ios::binary);
file.seekg(0,file.end);
tempFileContent.resize(file.tellg());
file.seekg(0,file.beg);
file.read(&tempFileContent[0],tempFileContent.size());
file.close();

// convert std::string -> std::wstring
std::wstringstream wss;
wss << tempFileContent.c_str();
std::wstring fileContent = wss.str();

// convert std::wstring -> QStringView
// - either work:
QStringView qstrv2(fileContent.data(),fileContent.size());

// convert to label
QLabel* someLabel = new QLabel(qstrv2.toLatin1());    //!< WORKS                  
mainLayout->addWidget(someLabel);

/* these did not work:
// total garbage
QLabel(QString::fromUtf8(qstrv2.toString().toStdString().c_str()));

// misses just the 'ß'
QLabel* someLabel = new QLabel(qstrv2.toLocal8Bit());                                        
*/

文件内容所在的地方

a-b-c-ä-ö-ü-ß-0-1-2

有趣的是,将使用wifstream的文本文件读取为wstring所需的时间要比使用wstringstream将读取的字符串转换为wstring的时间长得多。 (这仍然比读取std :: string本身要慢得多)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-