如何将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本身要慢得多)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...