Class: QgsCodeEditor¶
A text editor based on QScintilla2.
Note
may not be available in Python bindings, depending on platform support
QgsCodeEditor¶
Class Hierarchy¶
Base classes¶
Subclasses¶
A CSS editor based on QScintilla2. |
|
A QGIS expression editor based on QScintilla2. |
|
A HTML editor based on QScintilla2. |
|
A Javascript editor based on QScintilla2. |
|
A JSON editor based on QScintilla2. |
|
A Python editor based on QScintilla2. |
|
A R stats code editor based on QScintilla2. |
|
A SQL editor based on QScintilla2. |
|
A shell script code editor based on QScintilla2. |
- class qgis.gui.QgsCodeEditor[source]¶
Bases:
QsciScintilla- __init__(parent: QWidget | None = None, title: str | None = '', folding: bool = False, margin: bool = False, flags: QgsCodeEditor.Flags | QgsCodeEditor.Flag = QgsCodeEditor.Flags(), mode: QgsCodeEditor.Mode = QgsCodeEditor.Mode.ScriptEditor)
Construct a new code editor.
- Parameters:
parent (Optional[QWidget] = None) – The parent QWidget
title (Optional[str] = '') – The title to show in the code editor dialog
folding (bool = False) –
False: Enable folding for code editor (deprecated, useflagsinstead)margin (bool = False) –
False: Enable margin for code editor (deprecated)flags (Union[QgsCodeEditor.Flags, QgsCodeEditor.Flag] = QgsCodeEditor.Flags()) – flags controlling behavior of code editor (since QGIS 3.28)
mode (QgsCodeEditor.Mode = QgsCodeEditor.Mode.ScriptEditor) – code editor mode (since QGIS 3.30)
- class Flag(*values)¶
Bases:
IntEnumFlags controlling behavior of code editor
Added in version 3.28.
CodeFolding: Indicates that code folding should be enabled for the editorImmediatelyUpdateHistory: Indicates that the history file should be immediately updated whenever a command is executed, instead of the default behavior of only writing the history on widget closeAdded in version 3.32.
- CodeFolding = 1¶
- ImmediatelyUpdateHistory = 2¶
- class Flags¶
- class Flags(f: QgsCodeEditor.Flags | QgsCodeEditor.Flag)
- class Flags(a0: QgsCodeEditor.Flags)
Bases:
object
- class MarginRole(*values)¶
Bases:
IntEnumMargin roles.
This enum contains the roles which the different numbered margins are used for.
Added in version 3.16.
LineNumbers: Line numbersErrorIndicators: Error indicatorsFoldingControls: Folding controls
- class Mode(*values)¶
Bases:
IntEnumCode editor modes.
Added in version 3.30.
ScriptEditor: Standard mode, allows for display and edit of entire scriptsOutputDisplay: Read only mode for display of command outputsCommandInput: Command input mode
- CommandInput = 2¶
- OutputDisplay = 1¶
- ScriptEditor = 0¶
- SEARCH_RESULT_INDICATOR: int = 34¶
- addWarning(self, lineNumber: int, warning: str | None)[source]¶
Adds a
warningmessage and indicator to the specified alineNumber.See also
Added in version 3.16.
- Parameters:
lineNumber (int)
warning (Optional[str])
- adjustScrollWidth(self)[source]¶
Adjust the width of the scroll bar to fit the content.
Added in version 3.42.
- virtual checkSyntax(self) bool[source]¶
Applies syntax checking to the editor.
This is only supported for editors which return the
Qgis.ScriptLanguageCapability.CheckSyntax capability fromlanguageCapabilities().Added in version 3.32.
- Return type:
bool
- clearPersistentHistory(self)[source]¶
Clears the entire persistent history of commands run in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- clearSessionHistory(self)[source]¶
Clears the history of commands run in the current session.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- clearWarnings(self)[source]¶
Clears all warning messages from the editor.
See also
Added in version 3.16.
- static color(role: QgsCodeEditorColorScheme.ColorRole) QColor[source]¶
Returns the color to use in the editor for the specified
role.This color will be the default theme color for the role, unless the user has manually selected a custom color scheme for the editor.
See also
Added in version 3.16.
- Parameters:
- Return type:
QColor
- static defaultColor(role: QgsCodeEditorColorScheme.ColorRole, theme: str | None = '') QColor[source]¶
Returns the default color for the specified
role.The optional
themeargument can be used to specify a colortheme. A blankthemeindicates the default color scheme.Available themes are stored in
QgsCodeEditorColorSchemeRegistry, and can be retrieved viaQgsGui.codeEditorColorSchemeRegistry().Added in version 3.16.
- Parameters:
theme (Optional[str] = '')
- Return type:
QColor
- signal editingTimeout[source]¶
pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- editingTimeoutInterval(self) int[source]¶
Returns the timeout (in milliseconds) threshold for the
editingTimeout()signal to be emitted after an edit.See also
Added in version 3.42.
- Return type:
int
- foldingVisible(self) bool[source]¶
Returns
Trueif the folding controls are visible in the editor.See also
- Return type:
bool
- static getMonospaceFont() QFont[source]¶
Returns the monospaced font to use for code editors.
Added in version 3.16.
- Return type:
QFont
- signal helpRequested[source]¶
pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- history(self) List[str][source]¶
Returns the list of commands previously executed in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- Return type:
List[str]
- virtual initializeLexer(self)[source]¶
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
The default implementation does nothing.
Added in version 3.16.
- insertText(self, text: str | None)[source]¶
Insert text at cursor position, or replace any selected text if user has made a selection.
- Parameters:
text (Optional[str]) – The text to be inserted
- interpreter(self) QgsCodeInterpreter | None[source]¶
Returns the attached code interpreter, or
Noneif not set.See also
Added in version 3.30.
- Return type:
Optional[QgsCodeInterpreter]
- isCursorOnLastLine(self) bool[source]¶
Returns
Trueif the cursor is on the last line of the document.Added in version 3.28.
- Return type:
bool
- static isFixedPitch(font: QFont) bool[source]¶
Returns
Trueif afontis a fixed pitch font.- Parameters:
font (QFont)
- Return type:
bool
- virtual language(self) Qgis.ScriptLanguage[source]¶
Returns the associated scripting language.
Added in version 3.30.
- Return type:
- virtual languageCapabilities(self) Qgis.ScriptLanguageCapabilities[source]¶
Returns the associated scripting language capabilities.
Added in version 3.32.
- Return type:
- static languageToString(language: Qgis.ScriptLanguage) str[source]¶
Returns a user-friendly, translated name of the specified script
language.Added in version 3.30.
- Parameters:
language (Qgis.ScriptLanguage)
- Return type:
str
- lexerColor(self, role: QgsCodeEditorColorScheme.ColorRole) QColor[source]¶
Returns the color to use in the lexer for the specified
role.Added in version 3.16.
- Parameters:
- Return type:
QColor
- lexerFont(self) QFont[source]¶
Returns the font to use in the lexer.
Added in version 3.16.
- Return type:
QFont
- lineNumbersVisible(self) bool[source]¶
Returns whether line numbers are visible in the editor.
See also
Added in version 3.16.
- Return type:
bool
- linearPosition(self) int[source]¶
Convenience function to return the cursor position as a linear index
Added in version 3.36.
- Return type:
int
- marginVisible(self) bool[source]¶
Returns whether margins are in a visible state
Deprecated since version 3.40: Use base class methods for individual margins instead, or
lineNumbersVisible().- Return type:
bool
- mode(self) QgsCodeEditor.Mode[source]¶
Returns the code editor mode.
Added in version 3.30.
- Return type:
- virtual moveCursorToEnd(self)[source]¶
Moves the cursor to the end of the document and scrolls to ensure it is visible.
Added in version 3.28.
- virtual moveCursorToStart(self)[source]¶
Moves the cursor to the start of the document and scrolls to ensure it is visible.
Added in version 3.28.
- signal persistentHistoryCleared[source]¶
pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- virtual populateContextMenu(self, menu: QMenu | None)[source]¶
Called when the context
menufor the widget is about to be shown, after it has been fully populated with the standard actions created by the base class.This method provides an opportunity for subclasses to add additional non-standard actions to the context menu.
Added in version 3.30.
- Parameters:
menu (Optional[QMenu])
- reformatCode(self)[source]¶
Applies code reformatting to the editor.
This is only supported for editors which return the
Qgis.ScriptLanguageCapability.Reformat capability fromlanguageCapabilities().Added in version 3.32.
- virtual reformatCodeString(self, string: str | None) str[source]¶
Applies code reformatting to a
stringand returns the result.This is only supported for editors which return the
Qgis.ScriptLanguageCapability.Reformat capability fromlanguageCapabilities().Added in version 3.32.
- Parameters:
string (Optional[str])
- Return type:
str
- removeHistoryCommand(self, index: int)[source]¶
Removes the command at the specified
indexfrom the history of the code editor.Added in version 3.30.
- Parameters:
index (int)
- runCommand(self, command: str | None, skipHistory: bool = False)[source]¶
Runs a command in the editor.
An
interpreter()must be set.Since QGIS 3.32, if
skipHistoryisTruethen the command will not be automatically added to the widget’s history.Added in version 3.30.
- Parameters:
command (Optional[str])
skipHistory (bool = False)
- runPostLexerConfigurationTasks(self)[source]¶
Performs tasks which must be run after a lexer has been set for the widget.
Added in version 3.16.
- selectionEnd(self) int[source]¶
Convenience function to return the end of the selection as a linear index Contrary to the getSelection method, this method returns the cursor position if no selection is made.
Added in version 3.36.
- Return type:
int
- selectionStart(self) int[source]¶
Convenience function to return the start of the selection as a linear index Contrary to the getSelection method, this method returns the cursor position if no selection is made.
Added in version 3.36.
- Return type:
int
- signal sessionHistoryCleared[source]¶
pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- static setColor(role: QgsCodeEditorColorScheme.ColorRole, color: QColor | Qt.GlobalColor)[source]¶
Sets the
colorto use in the editor for the specifiedrole.This color will be stored as the new default color for the role, to be used for all code editors.
Set
colorto an invalid QColor in order to clear the stored color value and reset it to the default color.See also
Added in version 3.16.
- Parameters:
color (Union[QColor, Qt.GlobalColor])
- setEditingTimeoutInterval(self, timeout: int)[source]¶
Sets the
timeout(in milliseconds) threshold for theeditingTimeout()signal to be emitted after an edit.See also
See also
Added in version 3.42.
- Parameters:
timeout (int)
- setFoldingVisible(self, folding: bool)[source]¶
Set whether the folding controls are visible in the editor.
See also
- Parameters:
folding (bool)
- setHistoryFilePath(self, path: str | None)[source]¶
Sets the file path to use for recording and retrieving previously executed commands.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- Parameters:
path (Optional[str])
- setInterpreter(self, newInterpreter: QgsCodeInterpreter | None)[source]¶
Sets an attached code interpreter for executing commands when the editor is in the QgsCodeEditor.Mode.CommandInput mode.
See also
Added in version 3.30.
- Parameters:
newInterpreter (Optional[QgsCodeInterpreter])
- setLineNumbersVisible(self, visible: bool)[source]¶
Sets whether line numbers should be visible in the editor.
Defaults to
False.See also
Added in version 3.16.
- Parameters:
visible (bool)
- setLinearPosition(self, position: int)[source]¶
Convenience function to set the cursor position as a linear index
Added in version 3.36.
- Parameters:
position (int)
- setLinearSelection(self, start: int, end: int)[source]¶
Convenience function to set the selection using linear indexes
Added in version 3.36.
- Parameters:
start (int)
end (int)
- setMarginVisible(self, margin: bool)[source]¶
Set margin visible state
- Parameters:
margin (bool) – Set margin in the editor
Deprecated since version 3.40: Use base class methods for individual margins instead, or
setLineNumbersVisible().
- setTitle(self, title: str | None)[source]¶
Set the widget title
- Parameters:
title (Optional[str]) – widget title
- showHistory(self)[source]¶
Shows the command history dialog.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- virtual showMessage(self, title: str | None, message: str | None, level: Qgis.MessageLevel)[source]¶
Shows a user facing message (eg a warning message).
The default implementation uses QMessageBox.
Added in version 3.32.
- Parameters:
title (Optional[str])
message (Optional[str])
level (Qgis.MessageLevel)
- showNextCommand(self)[source]¶
Shows the next command from the session in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- showPreviousCommand(self)[source]¶
Shows the previous command from the session in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- virtual toggleComment(self)[source]¶
Toggle comment for the selected text.
This is only supported for editors which return the
Qgis.ScriptLanguageCapability.ToggleComment capability fromlanguageCapabilities().Added in version 3.32.
- toggleLineComments(self, commentPrefix: str | None)[source]¶
Toggles comment for selected lines with the given comment prefix.
Added in version 3.44.
- Parameters:
commentPrefix (Optional[str])
- updatePrompt(self)[source]¶
Triggers an update of the interactive prompt part of the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
Added in version 3.30.
- updateSoftHistory(self)[source]¶
Updates the soft history by storing the current editor text in the history.
Added in version 3.30.