Qt Creator Documentation

Refactoring

Code refactoring is the process of changing the code without modifying the existing functionality of your application. By refactoring your code you can:

  • Improve internal quality of your application
  • Improve performance and extensibility
  • Improve code readability and maintainability
  • Simplify code structure

Finding Symbols

To find the use of a specific symbol in your project:

  1. In the editor place the cursor on the symbol and select Tools > C++ > Find Usages or press Ctrl+Shift+U.

    Qt Creator looks for the symbol in the following locations:

    • Files listed as a part of the project
    • Files directly used by the project files (for example, generated files)
    • Header files of used frameworks and libraries

    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.

  2. The Search Results pane opens and shows the location and number of instances of the symbol in the current project.

You can browse the search results in the following ways:

  • To go directly to an instance, double-click the instance in the Search Results pane.
  • To move between instances, click and in the Search Results pane.
  • To expand and collapse the list of all instances, click .
  • To clear the search results, click .

Renaming Symbols

The functions available for renaming symbols depend on whether you are writing C++ or QML code. For QML, you can only rename IDs.

To rename a specific symbol in a Qt project:

  1. In the editor, place the cursor on the symbol you would like to change and select Tools > C++ > Rename Symbol Under Cursor or press Ctrl+Shift+R.

    The Search Results pane opens and shows the location and number of instances of the symbol in the current project.

  2. To replace all selected instances, enter the name of the new symbol in the Replace with text box and click Replace.

    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.

To rename an ID in a Qt Quick project:

  1. Right-click an ID in the QML code and select Rename id.
  2. In the Rename id field, enter the new ID.

Applying Refactoring Actions

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

Refactoring C++ Code

You can apply the following types of refactoring actions to C++ code:

  • Change binary operands
  • Simplify if and when conditions (for example, move declarations out of if conditions)
  • Modify strings (for example, set the encoding for a string to Latin-1, mark strings translatable, and convert strings to camel case)
  • Create variable declarations
  • Create method declarations and definitions

Refactoring QML Code

You can apply the following types of refactoring actions to QML code:

  • Rename IDs
  • Split initializers
  • Move a QML element into a separate file to reuse it in other .qml files

[Previous: Finding and Replacing] [Next: Using Qt Quick Toolbars]

X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

s.parentNode.insertBefore(ga, s); })(); -->