Class: QgsCodeEditor¶
- class qgis.gui.QgsCodeEditor¶
Bases:
PyQt5.Qsci.QsciScintilla
A text editor based on QScintilla2.
Note
may not be available in Python bindings, depending on platform support
New in version 2.6.
QgsCodeEditor(parent: QWidget = None, title: str = ‘’, folding: bool = False, margin: bool = False, flags: Union[QgsCodeEditor.Flags, QgsCodeEditor.Flag] = QgsCodeEditor.Flags(), mode: QgsCodeEditor.Mode = QgsCodeEditor.Mode.ScriptEditor) Construct a new code editor.
- Parameters:
parent – The parent QWidget
title – The title to show in the code editor dialog
folding –
False
: Enable folding for code editor (deprecated, useflags
instead)margin –
False
: Enable margin for code editor (deprecated)flags – flags controlling behavior of code editor (since QGIS 3.28)
mode – code editor mode (since QGIS 3.30)
New in version 2.6:
Enums
Bases:
enum.IntEnum
Bases:
enum.IntEnum
Bases:
enum.IntEnum
Methods
Adds a
warning
message and indicator to the specified alineNumber
.Applies syntax checking to the editor.
Clears the entire persistent history of commands run in the editor.
Clears the history of commands run in the current session.
Clears all warning messages from the editor.
Returns the color to use in the editor for the specified
role
.- param event:
Returns the default color for the specified
role
.- param watched:
- param event:
Returns
True
if the folding controls are visible in the editor.Returns the monospaced font to use for code editors.
Returns the list of commands previously executed in the editor.
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
Insert text at cursor position, or replace any selected text if user has made a selection.
Returns the attached code interpreter, or
None
if not set.Returns
True
if the cursor is on the last line of the document.Returns
True
if afont
is a fixed pitch font.- param event:
Returns the associated scripting language.
Returns the associated scripting language capabilities.
Returns a user-friendly, translated name of the specified script
language
.Returns the color to use in the lexer for the specified
role
.Returns the font to use in the lexer.
Returns whether line numbers are visible in the editor.
Returns whether margins are in a visible state
Returns the code editor mode.
Moves the cursor to the end of the document and scrolls to ensure it is visible.
Moves the cursor to the start of the document and scrolls to ensure it is visible.
Called when the context
menu
for the widget is about to be shown, after it has been fully populated with the standard actions created by the base class.Applies code reformatting to the editor.
Applies code reformatting to a
string
and returns the result.Removes the command at the specified
index
from the history of the code editor.Runs a command in the editor.
Performs tasks which must be run after a lexer has been set for the widget.
scrollContentsBy
Sets the
color
to use in the editor for the specifiedrole
.Set whether the folding controls are visible in the editor.
Sets the file path to use for recording and retrieving previously executed commands.
Sets an attached code interpreter for executing commands when the editor is in the QgsCodeEditor.Mode.CommandInput mode.
Sets whether line numbers should be visible in the editor.
Set margin visible state
Set the widget title
Shows the command history dialog.
Shows a user facing message (eg a warning message).
Shows the next command from the session in the editor.
Shows the previous command from the session in the editor.
Toggle comment for the selected text.
Triggers an update of the interactive prompt part of the editor.
Updates the soft history by storing the current editor text in the history.
Stores the commands executed in the editor to the persistent history file.
Signals
pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL
pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL
- class Flag(value)¶
Bases:
enum.IntEnum
Flags controlling behavior of code editor
New 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 close. Since QGIS 3.32.
- CodeFolding = 1¶
- ImmediatelyUpdateHistory = 2¶
- baseClass¶
alias of
QgsCodeEditor
- class Flags¶
- class Flags(Union[QgsCodeEditor.Flags, QgsCodeEditor.Flag])
- class Flags(QgsCodeEditor.Flags)
Bases:
sip.wrapper
- baseClass¶
alias of
QgsCodeEditor
- class MarginRole(value)¶
Bases:
enum.IntEnum
Margin roles.
This enum contains the roles which the different numbered margins are used for.
New in version 3.16.
LineNumbers
: Line numbersErrorIndicators
: Error indicatorsFoldingControls
: Folding controls
- baseClass¶
alias of
QgsCodeEditor
- class Mode(value)¶
Bases:
enum.IntEnum
Code editor modes.
New 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¶
- baseClass¶
alias of
QgsCodeEditor
- actionEvent(self, QActionEvent)¶
- addWarning(self, lineNumber: int, warning: str)¶
Adds a
warning
message and indicator to the specified alineNumber
.See also
New in version 3.16.
- Parameters:
lineNumber (int) –
warning (str) –
- canInsertFromMimeData(self, QMimeData) bool ¶
- changeEvent(self, QEvent)¶
- checkSyntax(self) bool ¶
Applies syntax checking to the editor.
This is only supported for editors which return the
Qgis
.ScriptLanguageCapability.CheckSyntax capability fromlanguageCapabilities()
.New in version 3.32.
- Return type:
bool
- childEvent(self, QChildEvent)¶
- clearPersistentHistory(self)¶
Clears the entire persistent history of commands run in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- clearSessionHistory(self)¶
Clears the history of commands run in the current session.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- clearWarnings(self)¶
Clears all warning messages from the editor.
See also
New in version 3.16.
- closeEvent(self, QCloseEvent)¶
- color(role: QgsCodeEditorColorScheme.ColorRole) QColor ¶
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
New in version 3.16.
- Parameters:
role (QgsCodeEditorColorScheme.ColorRole) –
- Return type:
QColor
- connectNotify(self, QMetaMethod)¶
- contextMenuEvent(self, event: QContextMenuEvent)¶
- Parameters:
event (QContextMenuEvent) –
- create(self, window: PyQt5.sip.voidptr = 0, initializeWindow: bool = True, destroyOldWindow: bool = True)¶
- customEvent(self, QEvent)¶
- defaultColor(role: QgsCodeEditorColorScheme.ColorRole, theme: str = '') QColor ¶
Returns the default color for the specified
role
.The optional
theme
argument can be used to specify a colortheme
. A blanktheme
indicates the default color scheme.Available themes are stored in
QgsCodeEditorColorSchemeRegistry
, and can be retrieved viaQgsGui.codeEditorColorSchemeRegistry()
.New in version 3.16.
- Parameters:
role (QgsCodeEditorColorScheme.ColorRole) –
theme (str = '') –
- Return type:
QColor
- destroy(self, destroyWindow: bool = True, destroySubWindows: bool = True)¶
- disconnectNotify(self, QMetaMethod)¶
- dragEnterEvent(self, QDragEnterEvent)¶
- dragLeaveEvent(self, QDragLeaveEvent)¶
- dragMoveEvent(self, QDragMoveEvent)¶
- drawFrame(self, QPainter)¶
- dropEvent(self, QDropEvent)¶
- enterEvent(self, QEvent)¶
- event(self, QEvent) bool ¶
- eventFilter(self, watched: QObject, event: QEvent) bool ¶
- Parameters:
watched (QObject) –
event (QEvent) –
- Return type:
bool
- focusInEvent(self, QFocusEvent)¶
- focusNextChild(self) bool ¶
- focusNextPrevChild(self, bool) bool ¶
- focusOutEvent(self, event: QFocusEvent)¶
- Parameters:
event (QFocusEvent) –
- focusPreviousChild(self) bool ¶
- foldingVisible(self) bool ¶
Returns
True
if the folding controls are visible in the editor.See also
- Return type:
bool
- fromMimeData(self, QMimeData) Tuple[QByteArray, bool] ¶
- getMonospaceFont() QFont ¶
Returns the monospaced font to use for code editors.
New in version 3.16.
- Return type:
QFont
- hideEvent(self, QHideEvent)¶
- history(self) List[str] ¶
Returns the list of commands previously executed in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- Return type:
List[str]
- initPainter(self, QPainter)¶
- initStyleOption(self, QStyleOptionFrame)¶
- initializeLexer(self)¶
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
The default implementation does nothing.
New in version 3.16.
- inputMethodEvent(self, QInputMethodEvent)¶
- inputMethodQuery(self, Qt.InputMethodQuery) Any ¶
- insertText(self, text: str)¶
Insert text at cursor position, or replace any selected text if user has made a selection.
- Parameters:
text (str) – The text to be inserted
- interpreter(self) QgsCodeInterpreter ¶
Returns the attached code interpreter, or
None
if not set.See also
New in version 3.30.
- Return type:
- isCursorOnLastLine(self) bool ¶
Returns
True
if the cursor is on the last line of the document.New in version 3.28.
- Return type:
bool
- isFixedPitch(font: QFont) bool ¶
Returns
True
if afont
is a fixed pitch font.- Parameters:
font (QFont) –
- Return type:
bool
- isSignalConnected(self, QMetaMethod) bool ¶
- keyPressEvent(self, event: QKeyEvent)¶
- Parameters:
event (QKeyEvent) –
- keyReleaseEvent(self, QKeyEvent)¶
- language(self) Qgis.ScriptLanguage ¶
Returns the associated scripting language.
New in version 3.30.
- Return type:
- languageCapabilities(self) Qgis.ScriptLanguageCapabilities ¶
Returns the associated scripting language capabilities.
New in version 3.32.
- Return type:
- languageToString(language: Qgis.ScriptLanguage) str ¶
Returns a user-friendly, translated name of the specified script
language
.New in version 3.30.
- Parameters:
language (Qgis.ScriptLanguage) –
- Return type:
str
- leaveEvent(self, QEvent)¶
- lexerColor(self, role: QgsCodeEditorColorScheme.ColorRole) QColor ¶
Returns the color to use in the lexer for the specified
role
.New in version 3.16.
- Parameters:
role (QgsCodeEditorColorScheme.ColorRole) –
- Return type:
QColor
- lexerFont(self) QFont ¶
Returns the font to use in the lexer.
New in version 3.16.
- Return type:
QFont
- lineNumbersVisible(self) bool ¶
Returns whether line numbers are visible in the editor.
See also
New in version 3.16.
- Return type:
bool
- marginVisible(self) bool ¶
Returns whether margins are in a visible state
Deprecated since version Use: base class methods for individual margins instead, or
lineNumbersVisible()
- Return type:
bool
- metric(self, QPaintDevice.PaintDeviceMetric) int ¶
- mode(self) QgsCodeEditor.Mode ¶
Returns the code editor mode.
New in version 3.30.
- Return type:
- mouseDoubleClickEvent(self, QMouseEvent)¶
- mouseMoveEvent(self, QMouseEvent)¶
- mousePressEvent(self, QMouseEvent)¶
- mouseReleaseEvent(self, QMouseEvent)¶
- moveCursorToEnd(self)¶
Moves the cursor to the end of the document and scrolls to ensure it is visible.
New in version 3.28.
- moveCursorToStart(self)¶
Moves the cursor to the start of the document and scrolls to ensure it is visible.
New in version 3.28.
- moveEvent(self, QMoveEvent)¶
- nativeEvent(self, Union[QByteArray, bytes, bytearray], PyQt5.sip.voidptr) Tuple[bool, int] ¶
- paintEvent(self, QPaintEvent)¶
- persistentHistoryCleared¶
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.
- Parameters:
name (str = ...) –
revision (int = ...) –
arguments (Sequence = ...) –
- Return type:
PYQT_SIGNAL
- populateContextMenu(self, menu: QMenu)¶
Called when the context
menu
for 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.
New in version 3.30.
- Parameters:
menu (QMenu) –
- receivers(self, PYQT_SIGNAL) int ¶
- reformatCode(self)¶
Applies code reformatting to the editor.
This is only supported for editors which return the
Qgis
.ScriptLanguageCapability.Reformat capability fromlanguageCapabilities()
.New in version 3.32.
- reformatCodeString(self, string: str) str ¶
Applies code reformatting to a
string
and returns the result.This is only supported for editors which return the
Qgis
.ScriptLanguageCapability.Reformat capability fromlanguageCapabilities()
.New in version 3.32.
- Parameters:
string (str) –
- Return type:
str
- removeHistoryCommand(self, index: int)¶
Removes the command at the specified
index
from the history of the code editor.New in version 3.30.
- Parameters:
index (int) –
- resizeEvent(self, QResizeEvent)¶
- runCommand(self, command: str, skipHistory: bool = False)¶
Runs a command in the editor.
An
interpreter()
must be set.Since QGIS 3.32, if
skipHistory
isTrue
then the command will not be automatically added to the widget’s history.New in version 3.30.
- Parameters:
command (str) –
skipHistory (bool = False) –
- runPostLexerConfigurationTasks(self)¶
Performs tasks which must be run after a lexer has been set for the widget.
New in version 3.16.
- QgsCodeEditor.scrollContentsBy(self, int, int)
- sender(self) QObject ¶
- senderSignalIndex(self) int ¶
- sessionHistoryCleared¶
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.
- Parameters:
name (str = ...) –
revision (int = ...) –
arguments (Sequence = ...) –
- Return type:
PYQT_SIGNAL
- setColor(role: QgsCodeEditorColorScheme.ColorRole, color: QColor | Qt.GlobalColor | QGradient)¶
Sets the
color
to 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
color
to an invalid QColor in order to clear the stored color value and reset it to the default color.See also
New in version 3.16.
- Parameters:
role (QgsCodeEditorColorScheme.ColorRole) –
color (Union[QColor) –
- setFoldingVisible(self, folding: bool)¶
Set whether the folding controls are visible in the editor.
See also
- Parameters:
folding (bool) –
- setHistoryFilePath(self, path: str)¶
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.
New in version 3.30.
- Parameters:
path (str) –
- setInterpreter(self, newInterpreter: QgsCodeInterpreter)¶
Sets an attached code interpreter for executing commands when the editor is in the QgsCodeEditor.Mode.CommandInput mode.
See also
New in version 3.30.
- Parameters:
newInterpreter (QgsCodeInterpreter) –
- setLineNumbersVisible(self, visible: bool)¶
Sets whether line numbers should be visible in the editor.
Defaults to
False
.See also
New in version 3.16.
- Parameters:
visible (bool) –
- setMarginVisible(self, margin: bool)¶
Set margin visible state
- Parameters:
margin (bool) – Set margin in the editor
Deprecated since version Use: base class methods for individual margins instead, or
setLineNumbersVisible()
- setTitle(self, title: str)¶
Set the widget title
- Parameters:
title (str) – widget title
- QgsCodeEditor.setViewportMargins(self, int, int, int, int)
- setViewportMargins(self, QMargins) None ¶
- showEvent(self, QShowEvent)¶
- showHistory(self)¶
Shows the command history dialog.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- showMessage(self, title: str, message: str, level: Qgis.MessageLevel)¶
Shows a user facing message (eg a warning message).
The default implementation uses QMessageBox.
New in version 3.32.
- Parameters:
title (str) –
message (str) –
level (Qgis.MessageLevel) –
- showNextCommand(self)¶
Shows the next command from the session in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- showPreviousCommand(self)¶
Shows the previous command from the session in the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- tabletEvent(self, QTabletEvent)¶
- timerEvent(self, QTimerEvent)¶
- toMimeData(self, Union[QByteArray, bytes, bytearray], bool) QMimeData ¶
- toggleComment(self)¶
Toggle comment for the selected text.
This is only supported for editors which return the
Qgis
.ScriptLanguageCapability.ToggleComment capability fromlanguageCapabilities()
.New in version 3.32.
- updateMicroFocus(self)¶
- updatePrompt(self)¶
Triggers an update of the interactive prompt part of the editor.
Note
Applies to code editors in the QgsCodeEditor.Mode.CommandInput mode only.
New in version 3.30.
- updateSoftHistory(self)¶
Updates the soft history by storing the current editor text in the history.
New in version 3.30.
- viewportEvent(self, QEvent) bool ¶
- viewportMargins(self) QMargins ¶
- viewportSizeHint(self) QSize ¶
- wheelEvent(self, QWheelEvent)¶
- writeHistoryFile(self) bool ¶
Stores the commands executed in the editor to the persistent history file.
New in version 3.30.
- Return type:
bool