如何将std::string_view转换成QStringView?

5
我尝试了以下转换方式,但是所有的方式都给我返回一个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::vector来存储字段的std::string_view。现在,我想在Qt GUI中将字符串可视化(实际上瞄准QTableWidgetItem),并认为QStringView对GUI部分可能很有用。我想保持BusinessLogic与GUI独立,因此避免在那里包含任何Qt库。

背景

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

错误

(我删除了类似note: 候选构造函数(隐式复制构造函数)不可用:需要1个参数,但提供了2个note: 候选构造函数模板不可用:需要单个参数'str',但提供了2个参数等)

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: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> >]

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 *]

2
你不能这样做。QStringView假定数据具有特定的格式,而std::string_view则有另一种格式。你需要转换为QString。 - spectras
5
std::string_view存储一个ANSI或UTF-8字符串,因此无法转换为操作UTF-16的QStringView。你只能将std::u16string_view(或者如果在你的平台上sizeof(wchar_t) == 2,则为std::wstring_view)转换为QStringView。 - phuclv
@phuclv 谢谢你提供的信息,我明天会查看wstring_view和u16string_view。 - Martin
1个回答

1
感谢您指引我使用 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慢得多)

1
不需要在传递给 QStringView 之前构造 wstring_view。直接使用 QStringView qstrv2a(somewstring.data(), somewstring.size()); 即可。 - phuclv
确实,但在我的情况下,我想在业务逻辑中使用wstring_views,并仅将QStringViews用于GUI表示。 - Martin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接