QExpressionEvaluator Class Reference
|
Operator | Description |
---|---|
( ) - ! | Brackets to control evaluation order of expression, unary minus, logical negation. eg. "2 * (3 + 4)" "-20" "!false" |
* / % | Arithmetic multiplication, division and modulus. eg. "2 * 3 / 3 % 3" |
+ - . | Arithmetic addition and subtraction. String concatenation. eg. "2 + 3 - 4" ""hello" . "world"" |
< > <= >= | Logical comparison. eg "10.9 > = 10.8" |
== != | Logical equality. eg. "true != false" |
&& | Logical AND eg. "true && true" |
|| | Logical OR eg. "false || true" |
Operators in the expression evaluator work with operands of the same type ie. the '+' operator can only add two integer or two real numbers. However, a user may still mix data types and the operands will be coerced to the correct type for the operator.
Consider the expression: 2 + "2"
The '+' operator adds two number operands, but the RHS operand is a string. As the LHS is an integer, the expression evaluator tries to coerce the RHS string to an integer to meet the requirements of the '+' operator. In this case, the coercion would succeed and "2" would simply become an integer 2. The result of the expression would then be the integer 4.
If operands cannot be coerced to the required types for an operator, then for static coercion the setup phase fails and isValid() returns false and for run-time coercion evaluate() returns false.
Run-time coercion failures can occur for 2 reasons:
a term, eg. a key in the valuespace, does not return a value that can be coerced to the required type, or a return value of one side of the expression cannot be coerced to the required type. eg. 2 + ("2" . "@/value/in/valuespace") would fail at run-time because the RHS of '+' is a string, calculated at runtime, that cannot be coerced to an integer.
Below are the data type requirements of the expression evaluator's operators.
Operator | Required operand types | Return type |
---|---|---|
Basic arithmetic (* / + -) | Alteast 1 double or integer. | / always returns a real, others return real or integer depending on operands. |
Modulus (%) | Two integers. | integer |
String Concatentation (.) | Atleast 1 string. | string |
Logical Comparison (< > <= >=) | Atleast 1 double, integer or bool. | bool |
Other Logical (== != && || !) | All types. | bool |
The expression evaluator has the ability to use values from the Qt Extended valuespace in its expressions.
2 + @/Value/Space/Key
The above code would add 2 and the value for /Value/Space/Key in the valuespace.
The '@' character uniquely identifies the subsequent characters as a valuespace key to the expression evaluator.
In the above example, /Value/Space/Key must return data that can be converted to an integer. If conversion fails, a run-time error will occur and evaluate() will return false.
If a value in the valuespace changes, expressions which use that value will emit the termsChanged() signal.
Controls the floating point format used by the expression evaluator.
Constant | Value | Description |
---|---|---|
QExpressionEvaluator::Double | 0 | C++ double data type. |
QExpressionEvaluator::FixedPoint | 1 | Fixed point arithmetic using an 8byte integer. |
Constructs a QExpressionEvaluator. parent is passed to the QObject constructor.
Constructs a QExpressionEvaluator.
expr is passed to setExpression(). parent is passed to the QObject constructor.
Destroys the QExpressionEvaluator.
Clears the set expression and any associated data structures.
The expression becomes invalid ie. isValid() returns false.
Evaluates the expression specified through setExpression().
isValid() must return true before you call this function, otherwise it will abort().
Returns true if the expression was successfully evaluated, or false if a run-time error occured.
A run-time error can occur due to a coercion error or divide by zero.
If this function returns true, the result of the evaluation can be retrieved using result().
Returns the current expression data the evaluator is operating on.
See also setExpression().
Returns the current floating point format.
See also setFloatingPointFormat().
Returns true if this expression is valid, otherwise false.
An expression is valid if it is not empty, it is syntactually and semantically correct, and a previous call to evaluate() did not result in a run-time error.
You must not call evaluate() unless this function returns true.
Returns the result of the last call to evaluate() as a QVariant.
Returns an empty QVariant if evaluate() has not yet been called.
Sets the expression for the evaluator to be expr
expr is parsed and semantically checked immediat
Returns true on success; otherwise returns false.
You should call isValid() after this function to determine whether the setup phase was completed successfully.
See also expression().
Sets the floating point format to be used by the expression evaluator to fmt.
The default is QExpressionEvaluator::Double, but setting QExpressionEvaluator::FixedPoint may give results appropriate for user level applications.
See also floatingPointFormat().
Emitted when a pluggable term in the expression changes.
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt qtextended4.4 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com