Refactor code to:
To find the use of a specific symbol or QML component in your Qt C++ or Qt Quick project:
Qt Creator looks for the symbol in the following locations:
Note: You can also select Edit > Find/Replace > Advanced Find > C++ Symbols to search for classes, methods, enums, and declarations either from files listed as part of the project or from all files that are used by the code, such as include files.
You can browse the search results in the following ways:
To rename a specific symbol in a Qt project:
The Search Results pane opens and shows the location and number of instances of the symbol in the current project.
To omit an instance, uncheck the check-box next to the instance.
Note: This action replaces all selected instances of the symbol in all files listed in the Search Results pane. You cannot undo this action.
Note: Renaming local symbols does not open the Search Results pane. The instances of the symbol are highlighted in code and you can edit the symbol. All instances of the local symbol are changed as you type.
Qt Creator allows you to quickly and conveniently apply actions to refactor your code by selecting them in a context menu. The actions available depend on the position of the cursor in the code editor and on whether you are writing C++ or QML code.
To apply refactoring actions to C++ code, right-click an operand, conditional statement, string, or name to open a context menu. In QML code, click an item ID or name.
In the context menu, select Refactoring and then select a refactoring action.
You can also press Alt+Enter to open a context menu that contains refactoring actions available in the current cursor position.
You can apply the following types of refactoring actions to C++ code:
The following table summarizes the refactoring actions for C++ code. The action is available when the cursor is in the position described in the Activation column.
Refactoring Action | Description | Activation |
---|---|---|
Add Curly Braces | Adds curly braces to an if statement that does not contain a compound statement. For example, rewritesif (a)
b;
as if (a) {
b;
}
| if |
Move Declaration out of Condition | Moves a declaration out of an if or while condition to simplify the condition. For example, rewritesif (Type name = foo()) {} as Type name = foo; if (name) {} | Name of the introduced variable |
Rewrite Condition Using || | Rewrites the expression according to De Morgan's laws. For example, rewrites:!a && !b as !(a || b) | && |
Rewrite Using operator | Rewrites an expression negating it and using the inverse operator. For example, rewrites:
| <= < > >= == != |
Split Declaration | Splits a simple declaration into several declarations. For example, rewrites:int *a, b; as int *a; int b; | Type name or variable name |
Split if Statement | Splits an if statement into several statements. For example, rewrites:if (something && something_else) { } as if (something) { if (something_else) { } } and if (something || something_else) x; with if (something) x; else if (something_else) x; | && || |
Swap Operands | Rewrites an expression in the inverse order using the inverse operator. For example, rewrites:a op b as b flipop a | <= < > >= == != && || |
Convert to Decimal | Converts an integer literal to decimal representation | Numeric literal |
Convert to Hexadecimal | Converts an integer literal to hexadecimal representation | Numeric literal |
Convert to Octal | Converts an integer literal to octal representation | Numeric literal |
Convert to Objective-C String Literal | Converts a string literal to an Objective-C string literal if the file type is Objective-C(++). For example, rewrites the following strings"abcd" QLatin1String("abcd") QLatin1Literal("abcd") as @"abcd"
| String literal |
Enclose in QLatin1Char() | Sets the encoding for a character to Latin-1, unless the character is already enclosed in QLatin1Char, QT_TRANSLATE_NOOP, tr, trUtf8, QLatin1Literal, or QLatin1String. For example, rewrites'a'
as QLatin1Char('a')
| String literal |
Enclose in QLatin1String() | Sets the encoding for a string to Latin-1, unless the string is already enclosed in QLatin1Char, QT_TRANSLATE_NOOP, tr, trUtf8, QLatin1Literal, or QLatin1String. For example, rewrites"abcd"
as QLatin1String("abcd")
| String literal |
Mark as Translatable | Marks a string translatable. For example, rewrites "abcd" with one of the following options, depending on which of them is available:tr("abcd") QCoreApplication::translate("CONTEXT", "abcd") QT_TRANSLATE_NOOP("GLOBAL", "abcd") | String literal |
#include Header File | Adds the matching #include statement for a forward-declared class or struct | Forward-declared class or struct |
Add Definition in ... | Inserts a definition stub for a function declaration either in the header file (inside or outside the class) or in the implementation file. For free functions, inserts the definition after the declaration of the function or in the implementation file. Qualified names are minimized when possible, instead of always being fully expanded. For example, rewritesClass Foo {
void bar();
};
as (inside class) Class Foo {
void bar() {
}
};
as (outside class) Class Foo { void bar(); }; void Foo::bar() { } as (in implementation file) // Header file Class Foo { void bar(); }; // Implementation file void Foo::bar() { } | Method name |
Add 'Function' Declaration | Inserts the member function declaration that matches the member function definition into the class declaration. The function can be public, protected, private, public slot, protected slot, or private slot. | Method name |
Switch with Next/Previous Parameter | Moves a parameter down or up one position in a parameter list. | Parameter in the declaration or definition of a function or method |
Extract Method | Moves the selected code to a new method and replaces the block of code with a call to the new method. Enter a name for the method in the Extract Function Refactoring dialog. | Block of code selected |
Add Local Declaration | Adds the type of an assignee, if the type of the right-hand side of the assignment is known. For example, rewritesa = foo();
as Type a = foo();
where Type is the return type of foo() | Assignee |
Convert to Camel Case | Converts a symbol name to camel case, where elements of the name are joined without delimiter characters and the initial character of each element is capitalized. For example, rewrites an_example_symbol as anExampleSymbol and AN_EXAMPLE_SYMBOL as AnExampleSymbol | Identifier |
Complete Switch Statement | Adds all possible cases to a switch statement of the type enum | Switch |
Generate Missing Q_PROPERTY Members | Adds missing members to a Q_PROPERTY:
| Q_PROPERTY |
Apply Changes | Keeps function declarations and definitions synchronized by checking for the matching declaration or definition when you edit a function signature and by applying the changes to the matching code. | Function signature. When this action is available, a light bulb icon appears: ![]() |
Add #include for undeclared identifier | Adds an #include directive to the current file to make the declaration of a symbol available. | Undeclared identifier |
Reformat Pointers or References | Reformats declarations with pointers or references according to the code style settings for the current project. In case no project is open, the current global code style settings are used. For example, rewrites:char*s; as char *s; When applied to selections, all suitable declarations in the selection are rewritten. | Declarations with pointers or references and selections containing such declarations |
Create Getter and Setter Member Functions | Creates getter and setter member functions for member variables. | Member variable in class definition |
Move Function Definition | Moves a function definition to the implementation file, outside the class or back to its declaration. For example, rewrites:class Foo { void bar() { // do stuff here } }; as class Foo { void bar(); }; void Foo::bar() { // do stuff here } | Function signature |
Assign to Local Variable | Adds a local variable which stores the return value of a function call or a new expression. For example, rewrites:QString s; s.loLatin1(); as QString s; QByteArray latin1 = s.toLatin1(); and new Foo;
as Foo * localFoo = new Foo; | Function call or class name |
Insert (Pure) Virtual Functions | Select an insertion mode:
| Class or base class name |
You can apply the following types of refactoring actions to QML code:
The following table summarizes the refactoring actions for QML code. The action is available when the cursor is in the position described in the Activation column.
Refactoring Action | Description | Activation |
---|---|---|
Move Component into 'filename.qml' | Moves a QML type into a separate file | QML type name |
Split Initializer | Reformats a one-line type into a multi-line type. For example, rewritesItem { x: 10; y: 20; width: 10 } as Item { x: 10; y: 20; width: 10 } | QML type property |
Wrap in Loader | Wraps the type in a Component type and loads it dynamically in a Loader type. This is usually done to improve startup time. | QML type name |
Add a message suppression comment | Prepends the line with an annotation comment that stops the message from being generated. | Error, warning or hint from static analysis |