aboutsummaryrefslogtreecommitdiffstats
path: root/docs/extensions.md
blob: 57a6f15e1b5e94c8a0e68522e827aa1ed2260b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Documentation for Python Extension Authors

This document is intended to provide an introduction to writing Python extensions
for Qt Creator.

## Importing Qt Creator specific modules
Currently, the bindings are available from Python under the following names:
```Python
from QtCreator import Utils # Imports Qt Creator's Utils helper library
from QtCreator import Core # Imports Qt Creator's Core plugin API
from QtCreator import PythonExtensions # Imports Python extension specific helper functions
```

## `QtCreator.PythonExtensions`
This is the Python binding for the extension manager that works on the C++ side. It provides the
following functions, that can be used from Python:

```Python
from QtCreator import PythonExtensions

# Returns a PySide QDir which points to the extension directory
PythonExtensions.extensionDir()

# Returns a list with the names of all Python extensions
# Each extension has properties 'name' and 'loaded'
PythonExtensions.extensionList()

# Returns the path of the custom location to
# where missing dependencies should be pip installed
PythonExtensions.pythonPackagePath()
```


# Qt Creator bindings
Generally, the parts of Qt Creator that are exposed have an interface that is nearly
identical to the C++ interface.

## Working with Menus
You can add new items to the menus of Qt Creator. You can either add an action directly, or
add a new action container, that holds a sub-menu.

### Adding a sub menu
The following code snippet illustrates how to add a new menu.

```Python
from QtCreator import Core

def hello():
    print("Hello World.")

# By convention, the menuId starts with "Python"
menuId = "Python.SmallMenu.Menu"

menu = Core.ActionManager.createMenu(menuId)
menu.menu().setTitle("My menu")
menu.menu().addAction("My action", hello)
# Add our new menu to the "Tools" menu in Qt Creator
Core.ActionManager.actionContainer("QtCreator.Menu.Tools").addMenu(menu)
```

### Adding a new action directly
The following code snippet illustrates how to add a new action to an existing action container.

```Python
from QtCreator import Core

def hello():
    print("Hello World.")

# Add a new action to the "Tools" menu
menu = Core.ActionManager.actionContainer("QtCreator.Menu.Tools")
menu.menu().addAction("My action", hello)
```

### Menu Id list
Currently, the following menu ids are available in Qt Creator.

```Python
"QtCreator.Menu.File"
"QtCreator.Menu.File.RecentFiles"
"QtCreator.Menu.Edit"
"QtCreator.Menu.Edit.Advanced"
"QtCreator.Menu.Tools"
"QtCreator.Menu.Tools.External"
"QtCreator.Menu.Window"
"QtCreator.Menu.Window.Panes"
"QtCreator.Menu.Window.ModeStyles"
"QtCreator.Menu.Window.Views"
"QtCreator.Menu.Help"
```


# The embedded Python interpreter

## Python modules and `sys.path`
When importing modules, the following important locations will be checked (in this order):

 1. The folder of the extension itself (files and folders in your extension)
 2. The system Python path entries (anything you `pip install`ed globally)
 3. Qt Creator's python extension directory
 4. The Qt Creator specific Python module directory
    - Note: This is where you should install any dependencies missing
      if you want to use non-standard Python packages / modules
    - This last path is accessible with `PluginInstance.pythonPackagePath()`

## Installing dependencies

If you want to install dependencies, you can include a standard Python `requirements.txt`
file in your extension folder. **Note that this file is pip installed _once_.** After the initial
pip install, a new file called `requirements.txt.installed` is created in the extensions folder. To
run the install again, this file simply has to be deleted. **Be careful to remove this file, when
distributing your extension in a .zip file.**

Otherwise you can do any setup you need in your extension's initialization code.