QLocale setDefault only works at 2nd time called - qt

I am trying to change the default language of a QLocale variable in my code and then use the different separators of each language. I do not want to change it for the whole system I just want to have a chance to print numbers with different group- and decimal separators.
The user can change the Decimal separator to what he or she prefers.
//this part works as expected in debugger
QLocale locale;
if(decSep==".")
{
locale.setDefault(QLocale::English);
}
else if(decSep==",")
{
locale.setDefault(QLocale::German);
}
else
{
locale.setDefault(QLocale::system().language());
}
//added for debug purposes
/*if(local.language()==QLocale::English)
{
int x=0;//jumped here when it was supposed to do so (decSep==".")
}*/
Now there is some code which I'm sure has nothing to do with this error.
Later I use:
//Now this doesn't work
QString tempNum1 = locale.toString(myNum, 'f');
With locale.toString I get the separators default to the given language.
Now my problem is that the locale variable seems to need some time or smth to change to other settings. When I change the decSep var and therefore the language is changed (I debugged this, this part is changed and when I ask for the language it gives the right enum) it uses the previously set settings. When I then call the function again, which gives me the tempNum1 string then it is working.
Is this a known problem or am I doing something wrong? Can I somehow updated locale or something like that?

You are setting the default locale, not the language of the current QLocale object.
Note that setDefault is a static function and therefore it does not change the object properties itself, i.e.
locale.setDefault(QLocale::English)
is the same as
QLocale::setDefault(QLocale::English)
Example
The following example may clarify this behaviour:
QLocale locale;
QLocale localeGerman(QLocale::German);
qDebug() << locale.toString(1.234, 'f'); // returns 1.234
qDebug() << localeGerman.toString(1.234, 'f'); // returns 1,234
QLocale::setDefault(QLocale::German); // same as locale.setDefault(QLocale::German);
qDebug() << locale.toString(1.234, 'f'); // returns still 1.234
QLocale locale2;
qDebug() << locale2.toString(1.234, 'f'); // returns 1,234
locale = localeGerman;
qDebug() << locale.toString(1.234, 'f'); // returns 1,234

Related

How to get correct QString from "\u0000"?

Sometimes I receive QString with value "\u0000" and I want to check if the received string equals to "\u0000".
But when I try:
qDebug() << QString("\u0000") << QString::fromUtf8("\u0000");
I get output: "\u0001" "\u0001"
So as a result I can not compare the strings. I see that initially this string was created using snprintf from byte to get char* and then created std::string and QString from std::string, but I can't reproduce it so far actually. But I feel like this is a workaround, but not the easy way to do it.
How do I create QString with value \u0000 using QString API and why QString("\u0000") results in value "\u0001"?
QString is always null-terminated. A null QString means that it has no data in its internal array but an empty QString has a single null character in its internal data array. So, if you have any control over the sender of the original array, it is better to send it by a QByteArray instead of a QString. You won't be able to count multiple null characters in a QString.
From QString reference document:
QString().isNull(); // returns true
QString().isEmpty(); // returns true
QString("").isNull(); // returns false
QString("").isEmpty(); // returns true
QString("abc").isNull(); // returns false
QString("abc").isEmpty(); // returns false
I figured out how to create such a string.
QString str(1, QChar('\0'));
or from std::string:
QString::fromStdString(std::string("\0", 1));
But I still don't know why QString("\u0000") results in "\u0001"

Qt QMap<int, MyClass> ignores insert command

I've a question that couldn't find anywhere. I have a QMap that's ignoring the QMap.insert(Key, Value) command. Here's the code:
//gets the selected problem index on the ProblemList
int selProblem = ui->tree_projects->currentItem()->data(0, Qt::UserRole).toInt();
//creates a new problem, sets its values and then replaces the old one on the ProblemsList variable
ProblemSets nProblem;
if(!problemsList.isEmpty()) //problemsList is an attribute of MainWindow
nProblem = problemsList.value(selProblem);
// some data collection that has been omitted because isn't important
// temporary maps that will carry the modifications
QMap<int, QString> nResName, nResType;
//data insertion into the maps
//these are fine
nResName.insert(fIdx, results_model->data(results_model->index(fIdx, 0)).toString());
nResType.insert(fIdx, results_model->data(results_model->index(fIdx, 1)).toString());
//replaces the old maps with the new ones
nProblem.SetProbResultsNames(nResName);
nProblem.SetProbResultsTypes(nResType);
//replaces the old problem with the new one
problemsList.insert(selProblem, nProblem); //this is the line that's doing nothing
}
That last line appears to be doing nothing! I've even tried to use
problemsList.remove(selProblem);
problemList.insert(selProblem, nProblem);
but got a similar result: the map not being inserted at the index selProblem. It got inserted, but with an outdated value - the same one of the deleted index -. I've checked on Debug and all the indexes and variables are correct, but when the .insert hits, nothing happens.
The most awkward thing is that this code is a copy/paste that I made from another method that I'm using that does similar thing, just changing the variable names, but that one works.
EDIT 1: This is the contents of nProblem, selProb and problemsList.value(selProblem)
Just before the Line:
problemsList.insert(selProblem, nProblem);
selProb: 0
nProblem:
ProbResultsNames: "NewRow0"
ProbResultsType: "Real"
problemsList.value(selProblem):
ProbResultsNames: non-existent
ProbResultsType: non-existent
After the line
problemsList.insert(selProblem, nProblem);
selProb: 0
nProblem:
ProbResultsNames: "NewRow0"
ProbResultsType: "Real"
problemsList.value(selProblem):
ProbResultsNames: non-existent
ProbResultsType: non-existent
EDIT 2:
class ProblemSets
{
public:
ProblemSets();
virtual ~ProblemSets();
ProblemSets(const ProblemSets& other);
ProblemSets& operator=(const ProblemSets& other);
//I hid getters and setters to avoid pollution on the post
private:
int index;
bool usingBenchmark;
QString functionSelected;
QString info;
QMap<int, QString> probVars_name, probVars_type, probResultsNames, probResultsTypes;
QMap<int, float> probVars_min, probVars_max;
QMap<int, int> probVars_stpSize, probVars_stp;
int varsNumber; // holds how many vars has been created, just for display purposes
int resNumber; // holds how many results has been created, just for display purposes
};
A simple test proves that QMap works as expected:
QMap<int, QString> mm;
mm.insert(1, "Test1");
qDebug() << mm[1]; // "Test1"
mm.remove(1);
qDebug() << mm[1]; // "" (default constructed value)
mm.insert(1, "Test2");
qDebug() << mm[1]; // "Test2"
Which means that the problem lies in your code.
This statement itself is highly suspicious:
That last line appears to be doing nothing!
Because then you go on to say that the map still contains the "old value". But you removed that key, so if the insert() method didn't work, you shouldn't be getting the old value, but a default constructed value.
Which means that the problem is most likely that nProblem has the same value as the one that is previously associated to that key in the map. The map works, you values are likely wrong.
Found the issue! I didn't have both the variables declared on the copy method of the ProblemSets class.
Solved simply adding them to the copy method
MainWindow::ProblemSets::ProblemSets(const ProblemSets& other)
{
// copy
index = other.index;
usingBenchmark = other.usingBenchmark;
functionSelected = other.functionSelected;
info = other.info;
probVars_name = other.probVars_name;
probVars_type = other.probVars_type;
probVars_min = other.probVars_min;
probVars_max = other.probVars_max;
probVars_stpSize = other.probVars_stpSize;
probVars_stp = other.probVars_stp;
//here
probResultsNames = other.probResultsNames;
probResultsTypes = other.probResultsTypes;
//
varsNumber = other.varsNumber;
resNumber = other.resNumber;
}
I had this issue before with the std::vector class, and that's why I suspected that could be that. Thanks to everyone that helped!

QMetaType::convert failed

I'm trying to use QMetaType::convert to convert a QJsonValue to another dynamic type. At first, I tested the following code with the dynamic type setting to QString, the conversion was failed.
QJsonValue value("test");
QString string;
if (!QMetaType::convert(&value, QMetaType::QJsonValue, &string, QMetaType::QString))
{
qDebug() << "failed";
}
Then, I found this static method to check whether the meta system has a registered conversion between two meta types.
qDebug() << QMetaType::hasRegisteredConverterFunction(QMetaType::QJsonValue, QMetaType::QString);
unfortunately, the result was false. Maybe QJsonValue is so complex that the conversion from QJsonValue to QString is not supported. Finally, I tried this, and the result was still false:
qDebug() << QMetaType::hasRegisteredConverterFunction(QMetaType::Int, QMetaType::Int);
It's odd, seems to be, Qt dose not implement the converter functions between basic meta types. And, users can't use QMetaType::registerConverter to register converter function between two basic meta types.
I still can't believe that Qt dosen't implement conversions between basic meta types, is there any initializtion or .pro setting I missed?
I guess you have the QMetaType system and the QVariant class to encapsulate Qt data types on the one hand, and QJsonValue to encapsulate a value in JSON on the other hand.
QMetaType::convert can deal with QVariant data only. What you can do is to extract the QVariant from your QJsonValue and then use the QMetaType system to convert your data you know being a QString.
// Your code
QJsonValue value("test");
QString string ;
if (!QMetaType::convert(&value, QMetaType::QJsonValue, &string, QMetaType::QString))
{
qDebug() << "failed";
}
// Extract the QVariant data
QVariant variant = value.toVariant() ;
// Two way to test conversion between QVariant and a type
qDebug() << "canConvert template:" << variant.canConvert<QString>() << endl
<< "canConvert parameter:" << variant.canConvert( QMetaType::QString ) ;
if( variant.canConvert<QString>() )
{
// Convert using QVariant methods
qDebug() << "QVariant toString:" << variant.toString() ;
// Convert Using QJsonValue methods
qDebug() << "QJsonValue toString:" << value.toString() ; // It's just a string representation of the data, not the actual data
}
outputs :
failed
canConvert template: true
canConvert parameter: true
QVariant toString: "test"
QJsonValue toString: "test"
PS: the QJsonValue::Type : String is different from the QVariant::Type : String (QMetaType::QString) so there is no relationship between them.

__LINE__ __FILE__ OR similar function in qml

I am trying to print caller function, line number and file name without throwing an error for normal debugging purpose in QML. I can print caller function name as follows
console.log("Caller Function Name"+arguments.callee.caller.name);
You can override qInstallMessageHandler default function and provide your custom function which also prints line number / caller. You can find an example in the linked documentation. Another partial example:
void loggingMessageHandler(QtMsgType type, const QMessageLogContext & context, const QString & msg)
{
QString timeStr(QDateTime::currentDateTime().toString("dd-MM-yy HH:mm:ss:zzz"));
QString contextString(QString("[%1 %2]").arg(context.file).arg(context.line));
mutex.lock();
QString level;
if(logFile.isOpen())
{
switch (type) {
case QtInfoMsg: level = QString("INF"); break;
case QtDebugMsg: level = QString("DEB"); break;
case QtWarningMsg: level = QString("WAR"); break;
case QtCriticalMsg: level = QString("CRT"); break;
case QtFatalMsg: level = QString("FTL"); break;
}
QTextStream stream(&logFile);
stream << timeStr << " " << contextString << "\t" << level << "\t" << msg << endl;
stream.flush();
}
#if defined(Q_OS_WIN)
OutputDebugString(reinterpret_cast<const wchar_t *>(level.append(' ' + msg + '\n').utf16()));
#elif defined(Q_OS_ANDROID)
android_default_message_handler(type, context, level.append(" " + msg));
#else // MACX || IOS || LINUX
fprintf(stderr, "%s\n", level.append(" " + msg).toLocal8Bit().constData());
#endif
mutex.unlock();
}
If logFile is open, logging data is wrote to that in a critical section delimited by a QMutex otherwise it is simply output to the standard output of each platform.
Whatever is the handler you define, it can be combined with categorized logging (available since Qt 5.2) to easily setup a custom logging facility tailored on your needs. You just need to define your logging categories, as described in this blog post, and call qCDebug, qCInfo(), qCWarning() and so on. Depending on the active categories (set via the static function setFilterRules() of QLoggingCategory) different logging info can be printed or skipped.
That's especially interesting now that Qt 5.8 is available. Since this release, you can use categories also in QML, i.e. you can call console functions and pass along a category, e.g.
function myFancyFunction() {
// foo code
console.log(myFancyCategory, "message");
// bar code
}
Also, categories declaration can be done fully in QML via the ad hoc type LoggingCategory.
ADDENDUM (Qt < 5.0)
The proposed solution works in a Qt 5.0+ environment with categories fully usable with Qt 5.3+ and QML categories available in Qt 5.8+; in a Qt 4.x environment you should override qInstallMsgHandler but you do not have a QMessageLogContext. That means you should manage file/line info outside the handler, e.g. you have to use Q_FUNC_INFO or rely on __FILE__ and __LINE__ in C++ (note that the latters have been removed in latest 5.x releases as e.g. discussed here).

Finding a specific character in a file in Qt

How can i find a specific character in a QFile which has a text in it?
for example i have ' $5000 ' written somewhere in my file. in want to find the "$" sign so i will realize that I've reached the number.
I tried using QString QTextStream::read(qint64 maxlen) by putting 1 as the maxlen :
QFile myfile("myfile.txt");
myfile.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream myfile_stream(&myfile);
while(! myfile_stream.atEnd())
{
if( myfile_stream.read(1) == '$')
{
qDebug()<<"found";
break;
}
}
and i get "error: invalid conversion from 'char' to 'const char* "
i also tried using the operator[] but apparently it can't be used for files.
Read in a line at a time and search the text that you've read in
QTextStream stream(&myFile);
QString line;
do
{
line = stream.readLine();
if(line.contains("$"))
{
qDebug()<<"found";
break;
}
} while (!line.isNull());
The error message you've posted doesn't match the issue in your code. Possibly the error was caused by something else.
QTextStream::read returns QString. You can't compare QString and const char* directly, but operator[] can help:
QString s = stream.read(1);
if (s.count() == 1) {
if (s[0] == '$') {
//...
}
}
However reading a file by too small pieces will be very slow. If your file is small enough, you can read it all at once:
QString s = stream.readAll();
int index = s.indexOf('$');
If your file is large, it's better to read file by small chunks (1024 bytes for example) and calculate the index of found character using indexOf result and count of already read chunks.
a single char could be read with
QTextStream myfile_stream(&myfile);
QChar c;
while (!myfile_stream.atEnd())
myfile_stream >> c;
if (c == '$') {
...
}
myfile_stream.read(1) - this is not good practice, you should not read from file one byte at a time. Either read the entire file, or buffered/line by line if there is a risk for the file to be too big to fit in memory.
The error you get is because you compare a QString for equality with a character literal - needless to say that is not going to work as expected. A string is a string even if there is only one character in it. As advised - use either the [] operator or better off for reading - QString::at() const which is guaranteed to create no extra copy. You don't use it on the QFile, nor on the QTextStream, but on the QString that is returned from the read() method of the text stream targeted at the file.
Once you have the text in memory, you can either use the regular QString methods like indexOf() to search for the index of a contained character.
in want to find the "$" sign so i will realize that I've reached the
number.
It sounds to me that you're searching for the '$' symbol because you're more interested in the dollar value that follows it. In this case, I suggest reading the files line by line and running them through a QRegExp to extract any values you're looking for.
QRegExp dollarFind("\\$(\\d+)");
while(!myfile_stream.atEnd()){
QString line = myfile_stream.readLine();
if (dollarFind.exactMatch(line)){
QStringList dollars = dollarFind.capturedTexts();
qDebug() << "Dollar values found: " << dollars.join(", ");
}
}

Resources