- Extending Qt Creator Manual
- QtCreator
- MacroExpander
- Extending Qt Creator Manual 18.0.2

# Utils::MacroExpander Class

class Utils::MacroExpander

The MacroExpander class manages Qt Creator wide variables, that a user can enter into many string settings. The variables are replaced by an actual value when the string is used, similar to how environment variables are expanded by a shell. More...

Header: | `#include <MacroExpander>`

- List of all members, including inherited members

## Public Functions

QString | expand(const QString &stringWithVariables) const
void | registerFileVariables(const QByteArray &prefix, const QString &heading, const Utils::MacroExpander::FileFunction &base, bool visibleInChooser = true, bool availableForExpansion = true)
void | registerIntVariable(const QByteArray &variable, const QString &description, const Utils::MacroExpander::IntFunction &value)
void | registerPrefix(const QByteArray &prefix, const QByteArray &examplePostfix, const QString &description, const Utils::MacroExpander::PrefixFunction &value, bool visible = true, bool availableForExpansion = true)
void | registerVariable(const QByteArray &variable, const QString &description, const Utils::MacroExpander::StringFunction &value, bool visibleInChooser = true, bool availableForExpansion = true)
QString | value(const QByteArray &variable, bool *found = nullptr) const
QString | variableDescription(const QByteArray &variable) const
QList<QByteArray> | visibleVariables() const

## Detailed Description

### Variables

Variable names can be basically any string without dollar sign and braces, though it is recommended to only use 7-bit ASCII without special characters and whitespace.

If there are several variables that contain different aspects of the same object, it is convention to give them the same prefix, followed by a colon and a postfix that describes the aspect. Examples of this are `CurrentDocument:FilePath` and `CurrentDocument:Selection`.

When the variable manager is requested to replace variables in a string, it looks for variable names enclosed in %{ and }, like %{CurrentDocument:FilePath}.

Environment variables are accessible using the %{Env:...} notation. For example, to access the SHELL environment variable, use %{Env:SHELL}.

Note: The names of the variables are stored as QByteArray. They are typically 7-bit-clean. In cases where this is not possible, UTF-8 encoding is assumed.

### Providing Variable Values

Plugins can register variables together with a description through registerVariable(). A typical setup is to register variables in the Plugin::initialize() function.

     void MyPlugin::initialize()
     {
         [...]
         MacroExpander::registerVariable(
             "MyVariable",
             Tr::tr("The current value of whatever I want."));
             [] {
                 QString value;
                 // do whatever is necessary to retrieve the value
                 [...]
                 return value;
             }
         );
         [...]
     }

For variables that refer to a file, you should use the convenience function MacroExpander::registerFileVariables(). The functions take a variable prefix, like `MyFileVariable`, and automatically handle standardized postfixes like `:FilePath`, `:Path` and `:FileBaseName`, resulting in the combined variables, such as `MyFileVariable:FilePath`.

### Providing and Expanding Parametrized Strings

Though it is possible to just ask the variable manager for the value of some variable in your code, the preferred use case is to give the user the possibility to parametrize strings, for example for settings.

(If you ever think about doing the former, think twice. It is much more efficient to just ask the plugin that provides the variable value directly, without going through string conversions, and through the variable manager which will do a large scale poll. To be more concrete, using the example from the Providing Variable Values section: instead of calling `MacroExpander::value("MyVariable")`, it is much more efficient to just ask directly with `MyPlugin::variableValue()`.)

#### User Interface

If the string that you want to parametrize is settable by the user, through a QLineEdit or QTextEdit derived class, you should add a variable chooser to your UI, which allows adding variables to the string by browsing through a list. See Utils::VariableChooser for more details.

#### Expanding Strings

Expanding variable values in strings is done by "macro expanders". Utils::AbstractMacroExpander is the base class for these, and the variable manager provides an implementation that expands Qt Creator variables through MacroExpander::macroExpander().

There are several different ways to expand a string, covering the different use cases, listed here sorted by relevance:

- Using MacroExpander::expandedString(). This is the most comfortable way to get a string with variable values expanded, but also the least flexible one. If this is sufficient for you, use it.
- Using the Utils::expandMacros() functions. These take a string and a macro expander (for which you would use the one provided by the variable manager). Mostly the same as MacroExpander::expandedString(), but also has a variant that does the replacement inline instead of returning a new string.
- Using Utils::CommandLine::expandMacros(). This expands the string while conforming to the quoting rules of the platform it is run on. Use this function with the variable manager's macro expander if your string will be passed as a command line parameter string to an external command.
- Writing your own macro expander that nests the variable manager's macro expander. And then doing one of the above. This allows you to expand additional "local" variables/macros, that do not come from the variable manager.

## Member Function Documentation

### QString MacroExpander::expand(const QString &stringWithVariables) const

Returns stringWithVariables with all variables replaced by their values. See the MacroExpander overview documentation for other ways to expand variables.

See also MacroExpander.

### void MacroExpander::registerFileVariables(const QByteArray &prefix, const QString &heading, const Utils::MacroExpander::FileFunction &base, bool visibleInChooser = true, bool availableForExpansion = true)

Convenience function to register several variables with the same prefix, that have a file as a value. Takes the prefix and registers variables like `prefix:FilePath` and `prefix:Path`, with descriptions that start with the given heading. For example `registerFileVariables("CurrentDocument", Tr::tr("Current Document"))` registers variables such as `CurrentDocument:FilePath` with description "Current Document: Full path including file name."

Takes a function that returns a FilePath as a base.

The variable is displayed to users if visibleInChooser is `true`. Set availableForExpansion to `false` if the variable should only be documented, but not actually get expanded.

See also registerVariable(), registerIntVariable(), and registerPrefix().

### void MacroExpander::registerIntVariable(const QByteArray &variable, const QString &description, const Utils::MacroExpander::IntFunction &value)

Makes the given integral-valued variable known to the variable manager, together with a localized description.

The value `IntFunction` is called to retrieve the current value of the variable.

See also registerVariable(), registerFileVariables(), and registerPrefix().

### void MacroExpander::registerPrefix(const QByteArray &prefix, const QByteArray &examplePostfix, const QString &description, const Utils::MacroExpander::PrefixFunction &value, bool visible = true, bool availableForExpansion = true)

Makes the given string-valued prefix known to the variable manager, together with a localized description. Provide an example for the value after the prefix in examplePostfix. That is used to show an expanded example in the variable chooser.

The value `PrefixFunction` will be called and gets the full variable name with the prefix stripped as input. It is displayed to users if visible is `true`. Set availableForExpansion to `false` if the variable should only be documented, but not actually get expanded.

See also registerVariable(), registerIntVariable(), and registerFileVariables().

### void MacroExpander::registerVariable(const QByteArray &variable, const QString &description, const Utils::MacroExpander::StringFunction &value, bool visibleInChooser = true, bool availableForExpansion = true)

Makes the given string-valued variable known to the variable manager, together with a localized description.

The value `StringFunction` is called to retrieve the current value of the variable. It is displayed to users if visibleInChooser is `true`. Set availableForExpansion to `false` if the variable should only be documented, but not actually get expanded.

See also registerFileVariables(), registerIntVariable(), and registerPrefix().

### QString MacroExpander::value(const QByteArray &variable, bool *found = nullptr) const

Returns the value of the given variable. If found is given, it is set to true if the variable has a value at all, false if not.

### QString MacroExpander::variableDescription(const QByteArray &variable) const

Returns the description that was registered for the variable.

### QList<QByteArray> MacroExpander::visibleVariables() const

Returns all registered variable names.

See also registerVariable() and registerFileVariables().