From 9ced5e92051cae2570510912d365d037d64554b6 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Wed, 25 Sep 2019 14:38:27 +0200 Subject: [PATCH] Fix QObject segfaults in QML CURA-6599 --- cura/Machines/QualityChangesGroup.py | 8 ++++++++ cura/Machines/QualityGroup.py | 11 ++++++++++- cura/Settings/MachineManager.py | 13 +++++++++---- .../Custom/QualitiesWithIntentMenu.qml | 11 ++++++++++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/cura/Machines/QualityChangesGroup.py b/cura/Machines/QualityChangesGroup.py index cd88368e2a..2bd0c0ad70 100644 --- a/cura/Machines/QualityChangesGroup.py +++ b/cura/Machines/QualityChangesGroup.py @@ -15,6 +15,14 @@ class QualityChangesGroup(QObject): def __init__(self, name: str, quality_type: str, intent_category: str, parent: Optional["QObject"] = None) -> None: super().__init__(parent) + + # CURA-6599 + # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to + # a QObject. Setting the object ownership to QQmlEngine.CppOwnership doesn't work, but setting the object + # parent to application seems to work. + from cura.CuraApplication import CuraApplication + self.setParent(CuraApplication.getInstance()) + self.name = name self.quality_type = quality_type self.intent_category = intent_category diff --git a/cura/Machines/QualityGroup.py b/cura/Machines/QualityGroup.py index 951d3717de..60f307555f 100644 --- a/cura/Machines/QualityGroup.py +++ b/cura/Machines/QualityGroup.py @@ -26,8 +26,17 @@ from cura.Machines.ContainerNode import ContainerNode # class QualityGroup(QObject): - def __init__(self, name: str, quality_type: str, parent = None) -> None: + def __init__(self, name: str, quality_type: str, parent: Optional["QObject"] = None) -> None: super().__init__(parent) + + # CURA-6599 + # Same as QualityChangesGroup. + # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to + # a QObject. Setting the object ownership to QQmlEngine.CppOwnership doesn't work, but setting the object + # parent to application seems to work. + from cura.CuraApplication import CuraApplication + self.setParent(CuraApplication.getInstance()) + self.name = name self.node_for_global = None # type: Optional[ContainerNode] self.nodes_for_extruders = {} # type: Dict[int, ContainerNode] diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index cb5b25c9ee..3aa2f68953 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -1599,12 +1599,17 @@ class MachineManager(QObject): @pyqtProperty(QObject, fset = setQualityChangesGroup, notify = activeQualityChangesGroupChanged) def activeQualityChangesGroup(self) -> Optional["QualityChangesGroup"]: global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack() - if not global_stack or global_stack.qualityChanges == empty_quality_changes_container: + if global_stack is None or global_stack.qualityChanges == empty_quality_changes_container: return None - for group in ContainerTree.getInstance().getCurrentQualityChangesGroups(): # Match on the container ID of the global stack to find the quality changes group belonging to the active configuration. + + all_group_list = ContainerTree.getInstance().getCurrentQualityChangesGroups() + the_group = None + for group in all_group_list: # Match on the container ID of the global stack to find the quality changes group belonging to the active configuration. if group.metadata_for_global and group.metadata_for_global["id"] == global_stack.qualityChanges.getId(): - return group - return None + the_group = group + break + + return the_group @pyqtProperty(bool, notify = activeQualityChangesGroupChanged) def hasCustomQuality(self) -> bool: diff --git a/resources/qml/PrintSetupSelector/Custom/QualitiesWithIntentMenu.qml b/resources/qml/PrintSetupSelector/Custom/QualitiesWithIntentMenu.qml index 5ebf653332..60fc8c55bf 100644 --- a/resources/qml/PrintSetupSelector/Custom/QualitiesWithIntentMenu.qml +++ b/resources/qml/PrintSetupSelector/Custom/QualitiesWithIntentMenu.qml @@ -172,7 +172,16 @@ Popup checkable: true visible: model.available text: model.name - checked: Cura.MachineManager.activeQualityChangesGroup.getName() == model.quality_changes_group.getName() + checked: + { + var active_quality_group = Cura.MachineManager.activeQualityChangesGroup + + if (active_quality_group != null) + { + return active_quality_group.getName() == model.quality_changes_group.getName() + } + return false + } ButtonGroup.group: buttonGroup } }