Need help to understand this QString symbol - qt

Here's the snippet:
QString fields = "(";
QList<QString> keys = data.keys();
...
for(int i = 0; i<keys.count(); i++)
{
...
fields += R"(")"+keys[i]+R"(")";
}
What does they symbol "R" do? I am new to Qt so I don't know if it's a QString feature or related to regex or probably a Macro defined somewhere? Any help is appreciated!

Related

Sort QList<SerialPortinfo> [duplicate]

I have the following datastructure.
QList<QVariant> fieldsList
How can I sort this list? This list contains strings. I want to sort the fieldList alphabetically?
In Qt5, it seems qSort is deprecated. It's recommended to use:
#include <algorithm>
QList<QVariant> fieldsList;
std::sort(fieldsList.begin(), fieldsList.end());
Reference: site
I would do sorting in the following way:
// Compare two variants.
bool variantLessThan(const QVariant &v1, const QVariant &v2)
{
return v1.toString() < v2.toString();
}
int doComparison()
{
[..]
QList<QVariant> fieldsList;
// Add items to fieldsList.
qSort(fieldsList.begin(), fieldsList.end(), variantLessThan);
}
Update:
in QT5 the qSort obsoleted. But it is still available to support old source codes. It is highly recommended to use std::sort instead of that in new codes.
int n;
int i;
for (n=0; n < fieldsList.count(); n++)
{
for (i=n+1; i < fieldsList.count(); i++)
{
QString valorN=fieldsList.at(n).field();
QString valorI=fieldsList.at(i).field();
if (valorN.toUpper() > valorI.toUpper())
{
fieldsList.move(i, n);
n=0;
}
}
}

Reading from .can files

I want to read .can files in Qt, I found out it is similiar to ini files so i used QSettings::IniFormat, i look for 2 attributes( say "rate" and "name").
code:
for(int i=0; i<files.count();i++)
{
QSettings file(files[i], QSettings::IniFormat);
QStringList keys = file.allKeys();
foreach(const QString& key, keys)
{
if(key.endsWith("/rate"))
{
QString Rate = file.value(key).toString();
qDebug() << Rate;
}
if(key.endsWith("/name"))
{
QString name = file.value(key).toString();
qDebug()<<name;
}
Problem is my can files has lot of "name" attribute, so this method is returning all the "name" attributes. I want to store the "name" attribute which the program finds right after "rate", there can be "name" attribute before "rate", so i just want to store the attribute which the program finds immediately after it finds "rate".
I don't know about .can files, I searched a little about them but couldn't find anything about them related to ini format.
Anyways, I rewrote your code to output the very first name attribute it finds after each rate encountered.
bool rateAttrFound = false;
for(int i=0; i<files.count();i++)
{
QSettings file(files[i], QSettings::IniFormat);
QStringList keys = file.allKeys();
foreach(const QString& key, keys)
{
if(key.endsWith("/rate"))
rateAttrFound = true;
if(key.endsWith("/name"))
{
if(rateAttrFound){
qDebug() << file.value(key).toString();
rateAttrFound = false;
}
}
}
}

Storing Variables in a Class

I am trying to keep track of a set of data by declaring a class.
The class is initialized with a unique ID but then fills the rest of the variables out later in the code after some calculations.
First, is that even an acceptable way to do this?
Second, I'm trying to pass it a char array but it does not want to take the value. Is this the correct way to define the fileName and call it back when creating the file?
Here's the example, I define a variable from the Customer class then try to store its filename:
#ifndef customer_h
#define customer_h
class Customer
{
public:
Customer (char *number);
char *ID;
double current;
double voltage;
double powerConsumption;
double remainingCredit;
int relay;
char *lastName;
char *firstName;
char *fileName;
private:
};
Customer::Customer(char *number)
{
ID = number;
}
#endif
void setup()
{
cust1.fileName = getFileName(cust1.ID);
}
char *getFileName(char *customerID)
{
char *charID;
String newID;
for (int i = strlen(customerID)-4; i<= strlen(customerID)-1; i++)
{
newID += customerID[i];
}
newID += ".csv";
int lenID = newID.length() + 1;
char fileName[lenID];
newID.toCharArray(fileName,lenID);
return fileName;
}
Thanks a lot in advance for any help and info you can provide!
Be better to use a structured rather than a class. They are pretty similar but in this case seems like a structure will be much better go and read about the differences and you'll see

How to sort QList<QVariant> in Qt?

I have the following datastructure.
QList<QVariant> fieldsList
How can I sort this list? This list contains strings. I want to sort the fieldList alphabetically?
In Qt5, it seems qSort is deprecated. It's recommended to use:
#include <algorithm>
QList<QVariant> fieldsList;
std::sort(fieldsList.begin(), fieldsList.end());
Reference: site
I would do sorting in the following way:
// Compare two variants.
bool variantLessThan(const QVariant &v1, const QVariant &v2)
{
return v1.toString() < v2.toString();
}
int doComparison()
{
[..]
QList<QVariant> fieldsList;
// Add items to fieldsList.
qSort(fieldsList.begin(), fieldsList.end(), variantLessThan);
}
Update:
in QT5 the qSort obsoleted. But it is still available to support old source codes. It is highly recommended to use std::sort instead of that in new codes.
int n;
int i;
for (n=0; n < fieldsList.count(); n++)
{
for (i=n+1; i < fieldsList.count(); i++)
{
QString valorN=fieldsList.at(n).field();
QString valorI=fieldsList.at(i).field();
if (valorN.toUpper() > valorI.toUpper())
{
fieldsList.move(i, n);
n=0;
}
}
}

Search QString character by character

I'm trying to parse a QString character by character with a while loop, but I can't figure out how to parse an individual character to char type. Here's my code, I know it's not optimal:
QString temp = (QString)t[0];
int i = 1;
while (t[i] != " ");
{
temp.append(t[i]);
i += 1;
}
I've seen the casting with toLocal8bit function, but whatever I try I just cannot adapt it to my code.
Qt Creator shows this error:
error: conversion from 'const char [2]' to 'QChar' is ambiguous
in line with the while function call
You can use C++ 11 range based for loop
for (auto chr : text)
{
if (!chr.isDigit()) // for exmpl.
return false;
}
Why don't you try that :
QString test = "test";
for(int i = 0; i< test.length(); i++)
{
if (test.at(i) != " ")
test.at(i).toLatin1();
}

Resources