I'm working with Qt on an existing project. I'm trying to send a string over a serial cable to a thermostat to send it a command. I need to make sure the string only contains 0-9, a-f, and is no more or less than 6 characters long. I was trying to use QString.contains, but I'm am currently stuck. Any help would be appreciated.
You have two options:
Use QRegExp
Use the QRegExp class to create a regular expression that finds what you're looking for. In your case, something like the following might do the trick:
QRegExp hexMatcher("^[0-9A-F]{6}$", Qt::CaseInsensitive);
if (hexMatcher.exactMatch(someString))
{
// Found hex string of length 6.
}
Update
Qt 5 users should consider using QRegularExpression instead of QRegExp:
QRegularExpression hexMatcher("^[0-9A-F]{6}$",
QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = hexMatcher.match(someString);
if (match.hasMatch())
{
// Found hex string of length 6.
}
Use QString Only
Check the length of the string and then check to see that you can convert it to an integer successfully (using a base 16 conversion):
bool conversionOk = false;
int value = myString.toInt(&conversionOk, 16);
if (conversionOk && myString.length() == 6)
{
// Found hex string of length 6.
}
Related
I have to read numbers from line edit in Qt Creator, and then divide them by 100 and display them in a label.
The only method I know is:
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
ui->label_OOP_marks->setText(QString(OOP_marks));
But the above method cannot read numbers; it reads string. I have tried a lot but can't figure out the code for this part of the program.
You can use QString::toDouble for converting string to number. If you are planning to accept integers use QString::toInt instead. To convert number to string again QString::number is way to go.
const auto& OOP_marks = ui->lineEdit_OOP_marks_input->text();
bool isNumber; // optional
const double number = OOP_marks.toDouble(&isNumber);
const auto& result = isNumber ? QString::number(number / 100) : QString("Please enter valid number.");
ui->label_OOP_marks->setText(result);
But I suggest you to use QSpinBox or QDoubleSpinBox, instead of QLineEdit.
How about QString toInt function, and then static QString::number function?
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
int OOP_marks_val = OOP_marks.toInt();
ui->label_OOP_marks->setText(QString::number(OOP_marks_val/100));
I want to print my string variable using printf method:
id = 6415F1BF713C
Serial.printf("id: %s\n\n", id);
Serial.print(id);
The result that I got was:
id: ⸮⸮⸮?
6415F1BF713C
is there any thing that's wrong?
Thanks.
Update :
//get device id
String getDeviceID() {
uint64_t chipid = ESP.getEfuseMac(); // The chip ID is essentially its MAC address(length: 6 bytes).
uint16_t chip = (uint16_t)(chipid >> 32);
char devID[40];
snprintf(devID, 40, "%04X%08X", chip, (uint32_t)chipid);
return devID;
}
String id = getDeviceID();
Serial.printf("id: %s\n\n", id);
Serial.print(id);
You didn't offer enough code to properly debug this, but I'm guessing what you mean is
String id = "6415F1BF713C";
Serial.printf("id: %s\n\n", id);
Serial.print(id);
The %s format in the printf() method expects to take a C/C++ char *, not a String. When you passed it a String, it printed the memory address of the String object - four characters which would appear as garbage, as you saw.
C and C++ use char * (pointers to characters) and char [] (arrays of characters) to represent strings. These are different from the Arduino String class, and people often confuse them.
To use the printf() method with a String you need to get its C string pointer, like this:
Serial.printf("id: %s\n\n", id.c_str());
The reason that this:
Serial.print(id);
works is that the print() method has a form that specifically expects to take a String object as an argument.
I have custom(dynamic QString) for example something like this 123+555 and i need to get this after +.Also there can be something different then + (/,*,- etc.). My question is how to get part of string after some char.
Use the split function, which allows you to specify the separator and returns a list of the elements.
QString string("123+555");
QStringList listItems = string.split('+', QString::SkipEmptyParts);
QString finalString = listItems[1];
Alternatively, you can find by index the separating character location and use that with a call to right
Since you're usin Qt, you could try the class: QRegExp.
With such class you could write code like this:
// This code was not tested.
QRegExp rx("(\\d+)(\\+|\\-|\\*|/)(\\d+)"); // Be aware, I recommend you to read the link above in order to see how construct the proper regular expression.
int pos = rx.indexIn("23+344");
if (pos > -1) {
QString number_1 = rx.cap(1); // "23"
QString op = rx.cap(2); // "+"
QString number_2 = rx.cap(3); // "344"
// ...
}
This way you don't have to write code to check which of the characters(operators) "+, -, *, /" is present to then perform a split on the string depending on what character was found.
I try to show a persian string in Qt:
QMessageBox msg;
QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();
but it shows the following error :
/home/msi/Desktop/VoMail
Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40:
error: no matching function for call to 'QString::fromUtf8(QString&)'
I want to use a string variable, and not a string directly.
How can I convert a QString variable to Utf8?
As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:
QMessageBox msg;
QString str = QString::fromUtf8("یا حسین");
msg.setText(str);
msg.exec();
its not good idea write like that
using this must be better
QString str(tr("ya hossein");
and use linguist and add persian translation file to your project http://qt-project.org/doc/qt-4.8/linguist-translators.html
and if you dont want use this, you must be sure your IDE or code editor (like qtcreator) use utf8 for saving files and just use
QString str("یا حسین");
it must be ok, i tested that so many times
I have the below code to read a light sensor, convert to lux, concat with "lux." and send it to my SmartThings cloud.
Ultimately I want a value sent to SmartThings formatted like lux.110
void checkLux() {
float logLux = analogRead(lightPIN) * logRange / rawRange;
int luxValue = pow(10, logLux);
String statusUpdate = "lux." + luxValue;
Serial.println(statusUpdate);
smartthing.send(statusUpdate);
delay(1000);
}
This above code spits out some weird combination of characters to the serial monitor and doesnt print lux. or the luxvalue.
If I add this line String luxString = "lux."; and modify the line below, it all works great. Any thoughts on why I need to declare this string separately. According to the documentation either should work fine.
Also if there are any suggestions on variable savings within this block of code. I am not that great at it yet.
As Arduino just uses C++ most C++ functions will also work so dont just limit yourself to Arduino's reference pages.
Apparently the String constructor doesn't support numbers, you must convert them using the String() function first as seen here.
Alternatively I think you can append a string like this:
String statusUpdate = "lux.";
statusUpdate += luxValue;
as seen here,
which is the same as using String's concat function.
statusUpdate.concat(luxValue);