I have a case which can be summarized like the code below:
#include <QCoreApplication>
#include <QStringList>
#include <QDir>
#include <QCryptographicHash>
#include <iostream>
#include <QDebug>
#include <QObject>
using namespace std;
QString Encode(const QString & filePath)
{
auto temp = filePath.toLocal8Bit();
qDebug() << QString("Encode 8 bit: ") << temp;
return temp.toPercentEncoding(" ");
}
QString Decode(const QString & filePath)
{
auto temp = filePath.toLocal8Bit();
qDebug() << QString("Decode 8 bit: ") << temp;
return QByteArray::fromPercentEncoding(temp);
}
int main()
{
QString raw = Decode(QString("K%C3%A4ssbohrer"));
qDebug() << QString("Raw:") << raw;
QString encoded = Encode(raw);
}
And this code outputs:
"Decode 8 bit: " "K%C3%A4ssbohrer"
"Raw:" "Kõssbohrer"
"Encode 8 bit: " "K\xE4ssbohrer"
As can be seen when I call toLocal8Bit() on a string containing unicode is ending up different with what I initially have. And this problem only happens in windows.
What should I do for making my Encode and Decode functions work mutually
both in windows and linux? I mean with Encode I want to return a QString which I can use Decode later to obtain the original QString which may contain unicode.
Related
Code:
using ColumnIndexVector = std::vector<int>;
using ByteVector = std::vector<BYTE>;
void CCreateReportDlg::GetColumnIndexesToExclude()
{
const CString strSection = theApp.GetActiveScheduleSection(_T("Options"));
ByteVector vData = theApp.GetProfileVector(strSection, _T("AssignStatesEx"));
ColumnIndexVector vTemp(vData.begin(), vData.end()); // This converts BYTE to int
m_vColumnIndexesToExclude = vTemp;
}
Is there any way to avoid the requirement for vTemp without manually iterating vData and casting from BYTE to int?
Yes, just use assign(). IDK if you need to use clear() as well, but probably not. Just step through the runtime code the first time to know.
m_vColumnIndexesToExclude.assign(vData.begin(), vData.end());
Here's a test program:
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
using ColumnIndexVector = std::vector<int>;
using ByteVector = std::vector<BYTE>;
int main(int argc, char* argv[])
{
cout << "Test" << endl;
ByteVector bytes = {'A', 'B', 'C', 'D'};
ColumnIndexVector colVector;
for ( auto _val: bytes)
{
cout << _val << endl;
}
colVector.assign(bytes.begin(), bytes.end());
for ( auto _val : colVector)
{
cout << _val << endl;
}
return 0;
}
I'm trying to collect QNetworkCookies in a QSet. I create the QSet QSet<QNetworkCookie> cookies
and then insert the cookies with
cookies.insert(cookie); The compiler then tells me, that there is no qHash for QNetworkCookie, so I added an inline method like you can see in the code listing below. The compiler now is happy, but the method never gets called and the set contains the same cookie multiple times. Can someone point me in the right direction?
#ifndef BROWSER_H
#define BROWSER_H
#include <QDebug>
#include <QMainWindow>
#include <QNetworkCookie>
#include <QObject>
#include <QWebEngineView>
class Browser : public QMainWindow
{
Q_OBJECT
private:
QWebEngineView *webEngine;
QSet<QNetworkCookie> cookies;
public:
explicit Browser(const QUrl &url);
protected slots:
void finishLoading(bool);
void handleCookieAdded(const QNetworkCookie &cookie);
};
inline uint qHash(const QNetworkCookie &key, uint seed)
{
uint hash = qHash(key.domain(), seed) ^ qHash(key.name(), seed);
qDebug() << "qHash " << key.domain() << " " << key.name() << " " << hash;
return hash;
}
#endif // BROWSER_H
I'm working with a buffer and I'm trying to get a string from it, but isnt working...
Example:
*void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
// Ok, I caught v value successfuly
QString s;
in >> s;
// Didnt work :<
}*
The string lenght is stored on 2 first bytes...
Thanks
If the string was not written as a QString, you need to read its length and content separately.
quint8 v;
in >> v;
quint16 length = 0;
in >> length;
// the string is probably utf8 or latin
QByteArray buffer(length, Qt::Uninitialized);
in.readRawData(buffer.data(), length);
QString string(buffer);
You might have to change the endianness of the QDataStream with QDataStream::setByteOrder before reading the 16-bit length.
We should really see the writing code and how you create the QDataStream. I tried with the following sample, and in this case your function works very well:
#include <QCoreApplication>
#include <QDebug>
#include <QDataStream>
#include <QBuffer>
void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
qDebug() << v;
// Ok, I caught v value successfuly
QString s;
in >> s;
qDebug() << s;
// Didnt work :<
}
int main(int argc, char ** argv) {
QCoreApplication a(argc, argv);
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
// write test data into the buffer
QDataStream out(&buffer);
quint8 ival = 42;
QString sval = "Qt";
out << ival;
out << sval;
// read back data
buffer.seek(0);
myFunc(out);
return a.exec();
}
Output when executed:
$ ./App
42
"Qt"
via server API's i'm getting contact information using member of structure:
char displayName[USERNAME_MAX_SIZE];
One of those names is a European name with special characters: "Per Spånt"
the structure of the displayName is as follows:
When I import 'displayName' into a QString via the "fromUtf8" function, I am getting the following QString:
How can I get the correct string into my QString without converting the special character into two weird characters?
FWIW, this is what works and doesn't work for me using Qt 4.7.4.
#include <iostream>
#include <string>
#include <QDebug>
#include <QString>
int main()
{
char name[7] = "Sp__nt";
name[2]=-61;
name[3]=-91;
std::cout << name << std::endl; // works
qDebug() << QString::fromUtf8( name ); // does not work
qDebug() << QString::fromAscii( name ); // works
qDebug() << QString::fromLatin1( name ); // works
qDebug() << QString::fromStdString( name ); // works
return 0;
}
I want to save a QList<int> to my QSettings without looping through it.
I know that I could use writeArray() and a loop to save all items or to write the QList to a QByteArray and save this but then it is not human readable in my INI file..
Currently I am using the following to transform my QList<int> to QList<QVariant>:
QList<QVariant> variantList;
//Temp is the QList<int>
for (int i = 0; i < temp.size(); i++)
variantList.append(temp.at(i));
And to save this QList<Variant> to my Settings I use the following code:
QVariant list;
list.setValue(variantList);
//saveSession is my QSettings object
saveSession.setValue("MyList", list);
The QList is correctly saved to my INI file as I can see (comma seperated list of my ints)
But the function crashes on exit.
I already tried to use a pointer to my QSettings object instead but then it crashes on deleting the pointer ..
QSettings::setValue() needs QVariant as a second parameter. To pass QList as QVariant, you have to declare it as a Qt meta type. Here's the code snippet that demonstrates how to register a type as meta type:
#include <QCoreApplication>
#include <QDebug>
#include <QMetaType>
#include <QSettings>
#include <QVariant>
Q_DECLARE_METATYPE(QList<int>)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaTypeStreamOperators<QList<int> >("QList<int>");
QList<int> myList;
myList.append(1);
myList.append(2);
myList.append(3);
QSettings settings("Moose Soft", "Facturo-Pro");
settings.setValue("foo", QVariant::fromValue(myList));
QList<int> myList2 = settings.value("foo").value<QList<int> >();
qDebug() << myList2;
return 0;
}
You might have to register QList as a meta-type of its own for it to work. This is a good starting point to read up on meta-types in Qt: http://qt.nokia.com/doc/4.6/qmetatype.html#details .
I was also struggling with this ... and I believe I now have a decent solution.
I hope this saves someone the trouble, it caused me.
#include <QCoreApplication>
#include <QSettings>
#include <QList>
#include <QDataStream>
#include <QVariant>
#include <QVariantList>
#include <QDebug>
#include <deque>
template <class T> static QVariant toVariant(const QList<T> &list)
{
QVariantList variantList;
variantList.reserve(list.size());
for (const auto& v : list)
{
variantList.append(v);
}
return variantList;
}
template <class T> static QList<T> toList(const QVariant &qv)
{
QList <T> dataList;
foreach(QVariant v, qv.value<QVariantList>()) {
dataList << v.value<T>();
}
return dataList;
}
void Gen()
{
QList<QString> data {"hello", "world","how", "are", "you"};
QList<int> ages {10,20,30,40};
QSettings setup("stuff.ini", QSettings::IniFormat);
setup.beginWriteArray("rules");
for (int groups=0;groups<3;groups++)
{
setup.setArrayIndex(groups);
setup.setValue("rule",QString("Rule-%1").arg(groups));
setup.setValue("data", toVariant (data));
setup.setValue("ages", toVariant (ages));
}
setup.endArray();
setup.sync();
}
void Read()
{
QSettings setupR("stuff.ini", QSettings::IniFormat);
int rule_count = setupR.beginReadArray("rules");
for (int groups=0;groups<rule_count;groups++)
{
setupR.setArrayIndex(groups);
QString nameRead = setupR.value("rule").toString();
QList<QString> dataRead = toList<QString>(setupR.value("data"));
qDebug() << "Rule " << groups;
qDebug() << "Rule Read" << nameRead;
qDebug() << "Data Read2" << dataRead;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Write
Gen();
// Read
Read();
exit(0);
//return a.exec();
}
You should end up with an INI file which looks like this ...
[rules]
1\ages=10, 20, 30, 40
1\data=hello, world, how, are, you
1\rule=Rule-0
2\ages=10, 20, 30, 40
2\data=hello, world, how, are, you
2\rule=Rule-1
3\ages=10, 20, 30, 40
3\data=hello, world, how, are, you
3\rule=Rule-2
size=3
The nice thing here is you can edit this outside of QT (Just be careful) ...