PDA

View Full Version : Open odt files via Qt and Libreoffice api



YuriyRusinov
26th March 2020, 08:02
Hello, Colleagues !

I have to read odt files and search tables in them using Qt and Libreoffice api. I open these files using Qt mechanism and receive there URLs as QString or std::string, but the libreoffice api uses there own types such as rtl::OUString, rtl::OString, because I have to use both Qt and Libreoffice types which way these strings can be converted to each others ?

d_stranz
26th March 2020, 16:58
If the Libreoffice API allows you to convert these string types into std:: wstring or std:: string types, these can be further converted into QString / QByteArray. It is possible that the rtl string types are simply alternate names for C++ std string types. You'll have to consult the Libreoffice API docs for that.

Edit: It looks like rtl:: OString might be equivalent to a QByteArray. The rtl:: OString:: getStr() method returns a "sal_Char" pointer, which is simply a char *. So you should be able to directly initialize a QString or QByteArray with this pointer.

YuriyRusinov
27th March 2020, 09:23
I didn't find support std::string or std::wstring in Libreoffice API and do it via append chars to OUStringBuffer


int nlen=fileUrl.toString().size();
OUStringBuffer buf;
for (int i=0; i<nlen; i++)
buf.append( fileUrl.toString().toStdString().at(i) );

ChristianEhrlicher
27th March 2020, 19:58
int nlen=fileUrl.toString().size();
OUStringBuffer buf;
for (int i=0; i<nlen; i++)
buf.append( fileUrl.toString().toStdString().at(i) );


Do you try to win the 'What's the most inefficient way to convert a string' contest? It does not event work when fileUrl contains non-Latin1 characters.

As d_stranz already mentioned, OString is a char string so I would simply use the ctor which takes a char*: https://www.openoffice.org/api/docs/cpp/ref/names/rtl/OString/o.html#OString-1240



const QByteArray ba = fileUrl.toString().toUtf8()
OString ostr = OString(ba.data(), ba.size());