How to print string literal and QString with qDebug?
You can use the following: qDebug().nospace() << “abc” << qPrintable(s) << “def”; The nospace() is to avoid printing out spaces after every argument (which is default for qDebug()).
You can use the following: qDebug().nospace() << “abc” << qPrintable(s) << “def”; The nospace() is to avoid printing out spaces after every argument (which is default for qDebug()).
You can use : int x = QString::compare(str1, str2, Qt::CaseInsensitive); // if strings are equal x should return 0
I guess you should use: QString::fromUtf8(const QByteArray &str) Or: QString::QString(const QByteArray &ba) to convert QByteArray to QString, then write it into file by QTextStream. After that, read file by QTextStream, use: QString::toUtf8() to convert QString to QByteArray. QString::QString(const QByteArray &ba) Constructs a string initialized with the byte array ba. The given byte array is converted … Read more
You can, creating an QStringList before iteration, like this: QStringList myOptions; myOptions << “goLogin” << “goAway” << “goRegister”; /* goLogin = 0 goAway = 1 goRegister = 2 */ Then: switch(myOptions.indexOf(“goRegister”)){ case 0: // go to login… break; case 1: // go away… break; case 2: //Go to Register… break; default: … break; }
See the Qt docs about QString::arg(): QString str; str = “%1 %2”; str.arg(“%1f”, “Hello”); // returns “%1f Hello”
Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes. To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: – QJsonObject jsonObj; // assume this has been populated with Json data QJsonDocument doc(jsonObj); QString … Read more
You can use this QString constructor for conversion from QByteArray to QString: QString(const QByteArray &ba) QByteArray data; QString DataAsString = QString(data);
You don’t have all digit characters in your string. So you have to split by space QString Abcd = “123.5 Kb”; Abcd.split(” “)[0].toInt(); //convert the first part to Int Abcd.split(” “)[0].toDouble(); //convert the first part to double Abcd.split(” “)[0].toFloat(); //convert the first part to float Update: I am updating an old answer. That was a … Read more
If you do not need to modify the substring, then you can use QStringRef. The QStringRef class is a read only wrapper around an existing QString that references a substring within the existing string. This gives much better performance than creating a new QString object to contain the sub-string. E.g. QString myString(“This is a string”); … Read more
QString::fromStdString(content) is better since it is more robust. Also note, that if std::string is encoded in UTF-8, then it should give exactly the same result as QString::fromUtf8(content.data(), int(content.size())).