我尝试了以下转换,但都给了我一个 no matching constructor for initialization of 'QStringView'
错误(评论反射(reflect)了我试图调用的构造函数):
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());
(我认为第 1 次或第 2 次转换可能会起作用,但出于失望而尝试了第 3 次和第 4 次转换。)std::string
s 为 QString
s 并创建 QStringView
从这些中,通过重用开始/结束位置? QStringView
插入到 QTableWidgetItem
中。对于这个问题,我也将不胜感激。) 语境
我将文件内容读出为
std::string
并根据分隔符将其拆分为字段行。为了有效地做到这一点,我生成了 std::vector
的 std::string_view存储字段。现在我想在 Qt GUI 中可视化字符串(实际上是针对 QTableWidgetItem
s)并认为 QStringView可能对 GUI 部分很有帮助。我想让 BusinessLogic 独立于 GUI,因此避免在那里包含任何 Qt 库。环境
CONFIG += c++17
错误
(我删除了
note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
、 note: 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: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 *]
最佳答案
感谢您将我指向 std::wstring
.我花了一段时间才明白 wchar_t
, wstring
, wcout
, wifstream
, ... 的东西,但最后我有以下代码工作:
注意:仍然需要CONFIG += 17
在 .pro 文件中,以下包括
#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 本身慢得多)
关于c++ - 如何将 std::string_view 转换为 QStringView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63832367/