Class: QgsCodeEditorWidget

A widget which wraps a QgsCodeEditor in additional functionality.

This widget wraps an existing QgsCodeEditor object in a widget which provides additional standard functionality, such as search/replace tools. The caller must create an unparented QgsCodeEditor object (or a subclass of QgsCodeEditor) first, and then construct a QgsCodeEditorWidget passing this object to the constructor.

Note

may not be available in Python bindings, depending on platform support

Added in version 3.38.

QgsCodeEditorWidget containing a QgsCodeEditorPython editor

QgsCodeEditorWidget containing a QgsCodeEditorPython editor

QgsCodeEditorWidget showing the search bar

QgsCodeEditorWidget showing the search bar

Class Hierarchy

Inheritance diagram of qgis.gui.QgsCodeEditorWidget

Base classes

QgsPanelWidget

Base class for any widget that can be shown as a inline panel

QWidget

QObject

QPaintDevice

Methods

addWarning

Adds a warning message and indicator to the specified a lineNumber.

clearWarnings

Clears all warning messages from the editor.

editor

Returns the wrapped code editor.

filePath

Returns the widget's associated file path.

hideSearchBar

Hides the search bar.

isSearchBarVisible

Returns True if the search bar is visible.

loadFile

Loads the file at the specified path into the widget, replacing the code editor's content with that from the file.

messageBar

Returns the message bar associated with the widget, to use for user feedback.

openInExternalEditor

Attempts to opens the script from the editor in an external text editor.

save

Saves the code editor content into the file path.

scrollbarHighlightController

Returns the scrollbar highlight controller, which can be used to add highlights in the code editor scrollbar.

setFilePath

Sets the widget's associated file path.

setReplaceBarVisible

Sets whether the replace bar is visible.

setSearchBarVisible

Sets whether the search bar is visible.

shareOnGist

Shares the contents of the code editor on GitHub Gist.

showSearchBar

Shows the search bar.

triggerFind

Triggers a find operation, using the default behavior.

Signals

filePathChanged

Emitted when the widget's associated file path is changed.

loadedExternalChanges

Emitted when the widget loads in text from the associated file to bring in changes made externally to the file.

searchBarToggled

Emitted when the visibility of the search bar is changed.

class qgis.gui.QgsCodeEditorWidget[source]

Bases: QgsPanelWidget

__init__(editor: QgsCodeEditor | None, messageBar: QgsMessageBar | None = None, parent: QWidget | None = None)

Constructor for QgsCodeEditorWidget, wrapping the specified editor widget.

Ownership of editor will be transferred to this widget.

If an explicit messageBar is specified then it will be used to provide feedback, otherwise an integrated message bar will be used.

Parameters:
addWarning(self, lineNumber: int, warning: str | None)[source]

Adds a warning message and indicator to the specified a lineNumber.

This method calls QgsCodeEditor.addWarning(), but also automatically adds highlights to the widget scrollbars locating the warning location.

See also

clearWarnings()

Parameters:
  • lineNumber (int)

  • warning (Optional[str])

clearWarnings(self)[source]

Clears all warning messages from the editor.

This method calls QgsCodeEditor.clearWarnings(), but also removes highlights from the widget scrollbars at the warning locations.

See also

addWarning()

editor(self) QgsCodeEditor | None[source]

Returns the wrapped code editor.

Return type:

Optional[QgsCodeEditor]

filePath(self) str[source]

Returns the widget’s associated file path.

See also

setFilePath()

Return type:

str

signal filePathChanged(path: str)[source]

Emitted when the widget’s associated file path is changed.

See also

setFilePath()

See also

filePath()

Parameters:

path (str)

hideSearchBar(self)[source]

Hides the search bar.

See also

showSearchBar()

isSearchBarVisible(self) bool[source]

Returns True if the search bar is visible.

Return type:

bool

loadFile(self, path: str | None) bool[source]

Loads the file at the specified path into the widget, replacing the code editor’s content with that from the file.

This automatically sets the widget’s filePath()

Returns True if the file was loaded successfully.

Parameters:

path (Optional[str])

Return type:

bool

signal loadedExternalChanges[source]

Emitted when the widget loads in text from the associated file to bring in changes made externally to the file.

messageBar(self) QgsMessageBar | None[source]

Returns the message bar associated with the widget, to use for user feedback.

Return type:

Optional[QgsMessageBar]

openInExternalEditor(self, line: int = -1, column: int = -1) bool[source]

Attempts to opens the script from the editor in an external text editor.

This requires that the widget has an associated filePath() set.

Optionally a target line and column number can be specified to open the editor at the corresponding location. (Not all external editors support this.) Line/column numbers of -1 indicate that the current cursor position should be used. A line number of 0 corresponds to the first line, and a column number of 0 corresponds to the first column.

Return type:

bool

Returns:

True if the file was opened successfully.

Parameters:
  • line (int = -1)

  • column (int = -1)

save(self, path: str | None = '') bool[source]

Saves the code editor content into the file path.

Return type:

bool

Returns:

False if the file path has not previously been set, or if writing the file fails.

Note

When the path is empty, the content will be saved to the current file path if not empty.

Added in version 3.38.2.

Parameters:

path (Optional[str] = '')

scrollbarHighlightController(self) QgsScrollBarHighlightController | None[source]

Returns the scrollbar highlight controller, which can be used to add highlights in the code editor scrollbar.

Return type:

Optional[QgsScrollBarHighlightController]

signal searchBarToggled(visible: bool)[source]

Emitted when the visibility of the search bar is changed.

Parameters:

visible (bool)

setFilePath(self, path: str | None)[source]

Sets the widget’s associated file path.

See also

loadFile()

See also

filePath()

Parameters:

path (Optional[str])

setReplaceBarVisible(self, visible: bool)[source]

Sets whether the replace bar is visible.

Parameters:

visible (bool)

setSearchBarVisible(self, visible: bool)[source]

Sets whether the search bar is visible.

See also

showSearchBar()

See also

hideSearchBar()

Parameters:

visible (bool)

shareOnGist(self, isPublic: bool) bool[source]

Shares the contents of the code editor on GitHub Gist.

Requires that the user has configured an API token with appropriate permission in advance.

Return type:

bool

Returns:

False if the user has not configured a GitHub personal access token.

Parameters:

isPublic (bool)

showSearchBar(self)[source]

Shows the search bar.

See also

hideSearchBar()

triggerFind(self)[source]

Triggers a find operation, using the default behavior.

This will automatically open the search bar and start a find operation using the default behavior, e.g. searching for any selected text in the code editor.