How to convert a std::string_view to a QStringView

Viewed 2085

I tried the following conversions, but all give me a no matching constructor for initialization of 'QStringView' error (comments reflect the constructor I was trying to call):

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());

(I thought the 1st or 2nd conversion might work, and tried the 3rd and 4th just out of disappointment.)

  • Can someone please point me to the right conversion? Do I miss something?
  • Or do I need to dublicate all std::strings as QStrings and create QStringViews from these, by reusing the begin/end positions?
  • ( Besides that, I did not now figure out, how to insert a QStringView into a QTableWidgetItem. I would appreciate any help on this problem as well. )

Context

I read out file content as std::string and split it into lines of fields based on separators. In order to do this efficiently, I generated a std::vector of std::string_view to store the fields. Now I want to visualize the strings in a Qt GUI (actually aiming at QTableWidgetItems) and thought QStringView might serve me well for the GUI part. I want to keep BusinessLogic independent of the GUI an therefore avoided any includes of Qt libraries there.

Setting

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

Errors

(I removed 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 and alike)

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

Thanks for pointing me towards std::wstring. It took me a while understand the wchar_t , wstring, wcout, wifstream, ... stuff, but finally I have the following code working:

Note: still requires CONFIG += 17 in .pro file and following includes

#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());

where either of the above constructors works fine.

Example using QStringView inside a QLabel

// 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);

Example from file to 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());                                        
*/

where file content was

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

interestingly reading a text file with wifstream to a wstring takes much longer than converting a read string to a wstring using wstringstream. (which is still much slower than reading to std::string itself)

Related