Skip to content

Commit 7d8fc19

Browse files
authored
Merge pull request #268 from blurstudio/QtPrintSupport
Misplaced members can't create Qt sub-modules
2 parents b2b0a40 + 316f83c commit 7d8fc19

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

Qt.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import shutil
4444

4545

46-
__version__ = "1.1.0.b10"
46+
__version__ = "1.1.0.b11"
4747

4848
# Enable support for `from Qt import *`
4949
__all__ = []
@@ -394,6 +394,16 @@
394394
"QGLFormat",
395395
"QGLWidget"
396396
],
397+
"QtPrintSupport": [
398+
"QAbstractPrintDialog",
399+
"QPageSetupDialog",
400+
"QPrintDialog",
401+
"QPrintEngine",
402+
"QPrintPreviewDialog",
403+
"QPrintPreviewWidget",
404+
"QPrinter",
405+
"QPrinterInfo"
406+
],
397407
"QtSql": [
398408
"QSql",
399409
"QSqlDatabase",
@@ -706,6 +716,14 @@
706716
"QtCore.Signal": "QtCore.Signal",
707717
"QtCore.Slot": "QtCore.Slot",
708718
"QtGui.QItemSelectionRange": "QtCore.QItemSelectionRange",
719+
"QtGui.QAbstractPrintDialog": "QtPrintSupport.QAbstractPrintDialog",
720+
"QtGui.QPageSetupDialog": "QtPrintSupport.QPageSetupDialog",
721+
"QtGui.QPrintDialog": "QtPrintSupport.QPrintDialog",
722+
"QtGui.QPrintEngine": "QtPrintSupport.QPrintEngine",
723+
"QtGui.QPrintPreviewDialog": "QtPrintSupport.QPrintPreviewDialog",
724+
"QtGui.QPrintPreviewWidget": "QtPrintSupport.QPrintPreviewWidget",
725+
"QtGui.QPrinter": "QtPrintSupport.QPrinter",
726+
"QtGui.QPrinterInfo": "QtPrintSupport.QPrinterInfo",
709727
},
710728
"PyQt4": {
711729
"QtGui.QAbstractProxyModel": "QtCore.QAbstractProxyModel",
@@ -717,6 +735,14 @@
717735
"QtCore.pyqtSignal": "QtCore.Signal",
718736
"QtCore.pyqtSlot": "QtCore.Slot",
719737
"QtGui.QItemSelectionRange": "QtCore.QItemSelectionRange",
738+
"QtGui.QAbstractPrintDialog": "QtPrintSupport.QAbstractPrintDialog",
739+
"QtGui.QPageSetupDialog": "QtPrintSupport.QPageSetupDialog",
740+
"QtGui.QPrintDialog": "QtPrintSupport.QPrintDialog",
741+
"QtGui.QPrintEngine": "QtPrintSupport.QPrintEngine",
742+
"QtGui.QPrintPreviewDialog": "QtPrintSupport.QPrintPreviewDialog",
743+
"QtGui.QPrintPreviewWidget": "QtPrintSupport.QPrintPreviewWidget",
744+
"QtGui.QPrinter": "QtPrintSupport.QPrinter",
745+
"QtGui.QPrinterInfo": "QtPrintSupport.QPrinterInfo",
720746
}
721747
}
722748

@@ -910,16 +936,34 @@ def _reassign_misplaced_members(binding):
910936
src_module, src_member = src.split(".")
911937
dst_module, dst_member = dst.split(".")
912938

939+
# Get the member we want to store in the namesapce.
913940
try:
914-
src_object = getattr(Qt, dst_module)
941+
dst_value = getattr(getattr(Qt, "_" + src_module), src_member)
915942
except AttributeError:
916-
# Skip reassignment of non-existing members.
917-
# This can happen if a request was made to
918-
# rename a member that didn't exist, for example
943+
# If the member we want to store in the namespace does not exist,
944+
# there is no need to continue. This can happen if a request was
945+
# made to rename a member that didn't exist, for example
919946
# if QtWidgets isn't available on the target platform.
947+
_log("Misplaced member has no source: {}".format(src))
920948
continue
921949

922-
dst_value = getattr(getattr(Qt, "_" + src_module), src_member)
950+
try:
951+
src_object = getattr(Qt, dst_module)
952+
except AttributeError:
953+
if dst_module not in _common_members:
954+
# Only create the Qt parent module if its listed in
955+
# _common_members. Without this check, if you remove QtCore
956+
# from _common_members, the default _misplaced_members will add
957+
# Qt.QtCore so it can add Signal, Slot, etc.
958+
msg = 'Not creating missing member module "{m}" for "{c}"'
959+
_log(msg.format(m=dst_module, c=dst_member))
960+
continue
961+
# If the dst is valid but the Qt parent module does not exist
962+
# then go ahead and create a new module to contain the member.
963+
setattr(Qt, dst_module, _new_module(dst_module))
964+
src_object = getattr(Qt, dst_module)
965+
# Enable direct import of the new module
966+
sys.modules[__name__ + "." + dst_module] = src_object
923967

924968
setattr(
925969
src_object,

examples/QtSiteConfig/QtSiteConfig.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def update_members(members):
88
Arguments:
99
members (dict): The members considered by Qt.py
1010
"""
11-
1211
# Contrived example used for unit testing. This makes the QtCore module
1312
# not accessible. I chose this so it can be tested everywhere, for a
1413
# more realistic example see the README
@@ -22,6 +21,7 @@ def update_misplaced_members(members):
2221
Arguments:
2322
members (dict): The members considered by Qt.py
2423
"""
24+
# Create Qt.QtGui.QColorTest that points to Qtgui.QColor for unit testing.
2525
members["PySide2"]["QtGui.QColor"] = "QtGui.QColorTest"
2626
members["PyQt5"]["QtGui.QColor"] = "QtGui.QColorTest"
2727
members["PySide"]["QtGui.QColor"] = "QtGui.QColorTest"
@@ -83,7 +83,9 @@ def wrapper(*args, **kwargs):
8383
wrapper.__doc__ = some_function.__doc__
8484
wrapper.__name__ = some_function.__name__
8585
return wrapper
86-
decorators.setdefault("QWidget", {})["windowTitleDecorator"] = \
86+
decorators.setdefault("QWidget", {})["windowTitleDecorator"] = (
8787
_widgetDecorator
88-
decorators.setdefault("QMainWindow", {})["windowTitleDecorator"] = \
88+
)
89+
decorators.setdefault("QMainWindow", {})["windowTitleDecorator"] = (
8990
_mainWindowDecorator
91+
)

examples/QtSiteConfig/main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def test():
2727

2828
# Test _misplaced_members is applied correctly
2929
from Qt import QtGui
30-
assert QtGui.QColorTest == QtGui.QColor, \
30+
assert QtGui.QColorTest == QtGui.QColor, (
3131
"QtGui.QColor was not mapped to QtGui.QColorTest"
32+
)
3233

3334
# Test _compatibility_members is applied correctly
3435
title = "Test Widget"
@@ -38,20 +39,23 @@ def test():
3839
wid.setWindowTitle(title)
3940

4041
# Verify that our simple remapping of QWidget.windowTitle works
41-
assert QtCompat.QWidget.windowTitleTest(wid) == title, \
42+
assert QtCompat.QWidget.windowTitleTest(wid) == title, (
4243
"Non-decorated function was added to QtCompat.QWidget"
44+
)
4345
# Verify that our decorated remapping of QWidget.windowTitle works
4446
check = "Test: {}".format(title)
45-
assert QtCompat.QWidget.windowTitleDecorator(wid) == check, \
47+
assert QtCompat.QWidget.windowTitleDecorator(wid) == check, (
4648
"Decorated method was not added to QtCompat.QWidget"
49+
)
4750

4851
# Verify that our decorated remapping of QMainWindow.windowTitle is
4952
# different than the QWidget version.
5053
win = QtWidgets.QMainWindow()
5154
win.setWindowTitle(title)
5255
check = "QMainWindow Test: {}".format(title)
53-
assert QtCompat.QMainWindow.windowTitleDecorator(win) == check, \
56+
assert QtCompat.QMainWindow.windowTitleDecorator(win) == check, (
5457
"Decorated method was added to QtCompat.QMainWindow"
58+
)
5559
# Suppress "app" imported but unused warning
5660
app
5761

membership.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,27 @@ def compare(dicts):
5151
return common_members
5252

5353

54-
def copy_qtgui_to_qtwidgets():
54+
def copy_qtgui_to_modules():
5555
"""Copies the QtGui list of PySide/PyQt4 into QtWidgets"""
5656

5757
pyside_filepath = PREFIX + '/PySide.json'
5858
pyqt4_filepath = PREFIX + '/PyQt4.json'
5959
pyside = read_json(pyside_filepath)
6060
pyqt4 = read_json(pyqt4_filepath)
6161

62+
# When Qt4 was moved to Qt5, they split QtGui into QtGui, QtWidgets, and
63+
# QtPrintSupport.
6264
pyside['QtWidgets'] = pyside['QtGui']
6365
pyqt4['QtWidgets'] = pyqt4['QtGui']
66+
pyside['QtPrintSupport'] = pyside['QtGui']
67+
pyqt4['QtPrintSupport'] = pyqt4['QtGui']
6468

6569
write_json(pyside, pyside_filepath)
66-
print('--> Copied QtGui to QtWidgets for ' + os.path.basename(
67-
pyside_filepath))
70+
print('--> Copied QtGui to QtWidgets and QtPrintSupport for {0}'.format(
71+
os.path.basename(pyside_filepath)))
6872
write_json(pyqt4, pyqt4_filepath)
69-
print('--> Copied QtGui to QtWidgets for ' + os.path.basename(
70-
pyqt4_filepath))
73+
print('--> Copied QtGui to QtWidgets and QtPrintSupport for {0}'.format(
74+
os.path.basename(pyqt4_filepath)))
7175

7276

7377
def sort_common_members():
@@ -127,7 +131,7 @@ def generate_common_members():
127131
(options, args) = parser.parse_args()
128132

129133
if options.copy:
130-
copy_qtgui_to_qtwidgets()
134+
copy_qtgui_to_modules()
131135

132136
elif options.generate:
133137
generate_common_members()

0 commit comments

Comments
 (0)