Skip to content

Commit a99d9fb

Browse files
committed
Add QtSiteConfig example adding windows specific QtAxContainer
This shows how to use QtSiteConfig to add this module as well as modifying the new `__extras__` misplaced member feature.
1 parent 03b531f commit a99d9fb

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
4+
IS_WIN = sys.platform == "win32"
5+
6+
7+
def update_members(members):
8+
# Add windows only `QWinEventNotifier` class
9+
if IS_WIN:
10+
members["QtCore"].append("QWinEventNotifier")
11+
12+
# Add the windows only QtAxContainer class. The PySideX bindings use
13+
# the QtAxContainer module name, so we can just add them here
14+
members["QtAxContainer"] = ["QAxBase", "QAxObject", "QAxWidget"]
15+
16+
17+
def update_misplaced_members(members):
18+
if IS_WIN:
19+
# Correctly map the Windows only QtAxContainer misplaced in PyQtX.
20+
# PySide6/2 uses the QtAxContainer module name, rename PyQt's module.
21+
for bnd in ("PyQt6", "PyQt5"):
22+
# We have to add "QAxContainer" to __extras__ so it's imported
23+
# by Qt.py and available for misplaced_members to access
24+
members[bnd].setdefault("__extras__", []).append("QAxContainer")
25+
# Add each individual misplaced member mapping to add to Qt.py
26+
members[bnd]["QAxContainer.QAxBase"] = "QtAxContainer.QAxBase"
27+
members[bnd]["QAxContainer.QAxObject"] = "QtAxContainer.QAxObject"
28+
members[bnd]["QAxContainer.QAxWidget"] = "QtAxContainer.QAxWidget"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## `QtSiteConfig_platforms` example
2+
3+
This example illustrates how to use QtSiteConfig to give access to platform specific Qt members.
4+
5+
**Usage**
6+
7+
```bash
8+
$ cd to/this/directory
9+
$ python main.py
10+
# Qt.QtCore was successfully removed by QSiteConfig.py
11+
```
12+
13+
Because `QtSiteConfig.py` is in the current working directory, it is available to import by Python. If running from a different directory, then you can append this directory to your `PYTHONPATH`
14+
15+
```bash
16+
$ set PYTHONPATH=path/to/QtSiteConfig/
17+
$ python main.py
18+
# Qt.QtCore was successfully removed by QSiteConfig.py
19+
```
20+
21+
> Linux and MacOS users: Replace `set` with `export`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Example of QtSiteConfig module used to modify exposed members of Qt.py"""
2+
3+
import os
4+
import sys
5+
6+
IS_WIN = sys.platform == "win32"
7+
print(f'Platform {"is" if IS_WIN else "is not"} windows: "{sys.platform}"')
8+
9+
10+
def test():
11+
"""QtCore is taken out of Qt.py via QtSiteConfig.py"""
12+
13+
# Expose this directory, and therefore QtSiteConfig, to Python
14+
sys.path.insert(0, os.path.dirname(__file__))
15+
16+
try:
17+
from Qt.QtCore import QWinEventNotifier # noqa: F401
18+
except ImportError:
19+
if IS_WIN:
20+
raise ImportError(
21+
"Qt.QtCore.QWinEventNotifier is not importable on windows"
22+
"update_members was not applied correctly."
23+
)
24+
else:
25+
print(
26+
"Qt.QtCore.QWinEventNotifier was not added on this "
27+
"non-windows platform"
28+
)
29+
else:
30+
if IS_WIN:
31+
print(
32+
"Qt.QtCore.QWinEventNotifier was added on windows via "
33+
"QtSiteConfig.py"
34+
)
35+
else:
36+
raise ImportError(
37+
"Qt.QtCore.QWinEventNotifier was importable, on this "
38+
"non-windows platform. This should be impossible."
39+
)
40+
41+
# Test _misplaced_members is applied correctly
42+
import Qt
43+
44+
assert not hasattr(Qt, "QAxContainer")
45+
if IS_WIN:
46+
assert Qt.QtAxContainer
47+
assert Qt.QtAxContainer.QAxBase
48+
assert Qt.QtAxContainer.QAxWidget
49+
print("Qt.QtAxContainer and members were added on windows.")
50+
else:
51+
assert not hasattr(Qt, "QtAxContainer")
52+
print("Qt.QtAxContainer and members was not added on non-windows.")
53+
54+
55+
if __name__ == "__main__":
56+
test()

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,4 @@ commands =
179179
# Testing examples..
180180
examples: python -m nose2 --verbose examples.loadUi.baseinstance1
181181
examples: python -m nose2 --verbose examples.QtSiteConfig.main
182+
examples: python -m nose2 --verbose examples.QtSiteConfig_platforms.main

0 commit comments

Comments
 (0)