QString function for only alpha characters? - qt

I am newbie in QT(4.7.4) and I am search for function, that checks an QString for alpha-characters and returns "true" if in this QString contains only characters.
Should I write this simple function myself? :( I hope it exists such function as isText() in VBA, but in Google and documentation I have not found it.
Thanks for answers and sorry for my english :)

You can simply validate the string with a QRegExp class matching an alphanumeric string. I suggest to use it with QValidator to be more clear.

You could use something like this (If your goal is to accept only strings, which contains a single character):
bool containsOnly(QString str, QChar c)
{
for(int i=0; i<str.length(); i++)
if(str.at(i)!=c)
return false;
return true;
}
and in use:
bool b = containsOnly("String", 'a');

Related

C++ QT Getting part from QString

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.

Qt Check QString to see if it is a valid hex value

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.
}

Convert QVector<char*> list into a QStringList

How can I convert a QVector<char*> to a QStringList?
There is no direct way.
QVector<char*> vector;
QStringList list;
foreach (const char * str, vector) {
list << str;
}
I think you can try do it in this way (it works for me if I have a QVector<QString>):
QVector<char*> vector_char;
QStringList myStringList = vector_char.toList();
This code has to work as QStringList has inherited members of QList.
Here is somenthing in the documentation: QVector documentation.
And that is all.
I hope it can help you.

How to call "QList<QVariant> QVariant::toList () const"

This is a pretty simple and probably dumb question, but I have forgotten how to use QList QVariant::toList () const
QVariant s = this->page()->mainFrame()->evaluateJavaScript (QString ("Open(%1,%2)").arg (point.x()).arg (point.y()));
List<QVariant> x;
x = s.toList ();
Of course this is wrong, what is the correct way out? :redface:
What you do is almost correct:
QList<QVariant> x = s.toList();
(Note the use of QList instead of List.)
What you're doing is right. May be you can check if the variant contains a list before converting it. E.g:
QVariant variant = list;
if(variant.canConvert(QVariant::List))
{
QList<QVariant> list_1 = variant.toList();
}

Closest match for Full Text Search

I am trying to implement an internal search for my website that can point users in the right direction in case the mistype a word, something like the did you mean : in google search.
Does anybody have an idea how such a search can be done? How can we establish the relevance of the word or the phrase we assume the user intended to search for?
i use asp.net and sql server 2005 with FTS (fullTextSearch)
Thank you
You could use an algorithm for determining string similarity and then suggest other string from your search index up to a certain difference.
One of these algorithms is the Levenshtein distance.
However, don't forget searching for existing solutions. I think e.g. Lucene has the capability to search for similar strings.
Btw, here's a related post on this topic: How does the Google “Did you mean?” Algorithm work?
This is done querying through regular expression the closest keywords that match the phrase.
Here is a great article that might help you.
With T-SQL You can use the SOUNDEX function to compare words phonetically.
If you take the users input and then compare it with other words in your database by soundex code, you should be able to come up with a list of 'do you mean'? words.
E.g.
select SOUNDEX('andrew')
select SOUNDEX('androo')
will both produce the same output (A536).
There are better algorithms these days, but soundex is built into sql server.
The simplest approach I can think of is to write a function that returns the degree of mismatch between two words, and you loop through all the words and find the best ones.
I've done this with a branch-and-bound method. Let me dig up the code:
bool matchWithinBound(char* a, char* b, int bound){
// skip over matching characters
while(*a && *b && *a == *b){a++; b++;}
if (*a==0 && *b==0) return true;
// if bound too low, quit
if (bound <= 0) return false;
// try assuming a has an extra character
if (*a && matchWithinBound(a+1, b, bound-1)) return true;
// try assuming a had a letter deleted
if (*b && matchWithinBound(a, b+1, bound-1)) return true;
// try assuming a had a letter replaced
if (*a && *b && matchWithinBound(a+1, b+1, bound-1)) return true;
// try assuming a had two adjacent letters swapped
if (a[0] && a[1]){
char temp;
int success;
temp = a[0]; a[0] = a[1]; a[1] = temp;
success = matchWithinBounds(a, b, bound-1);
temp = a[0]; a[0] = a[1]; a[1] = temp;
if (success) return true;
}
// can try other modifications
return false;
}
int DistanceBetweenWords(char* a, char* b){
int bound = 0;
for (bound = 0; bound < 10; bound++){
if (matchWithinBounds(a, b, bound)) return bound;
}
return 1000;
}
why don't you use google power?, you can consume their suggest service
here is an example on c#

Resources