PySide和PySide2之间的QKeySequence差异

时间:2018-12-08 21:44:10

标签: python qt pyside pyside2

由于我当前正在将gui应用程序从Python 2.7.14 / PySide 1.2.4移植到Python 3.7.1 / PySide2 5.11.2,因此我在使用快捷方式机制时遇到了一些问题。更具体地说:

    与PySide相比,
  • QKeySequence实例化似乎在PySide2中返回不同/令人惊讶的结果。
  • +枚举上的Qt.Key_*运算符似乎没有返回预期的结果。例如QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_Z)似乎无法正常工作。
  • 使用字符串代码实例化QKeySequences时,它可以工作,例如QKeySequence("Shift+Ctrl+Z")似乎可以正常工作。
  • 使用QKeySequence.StandardKeyQKeySequence(QKeySequence.Redo)这样的实例只会在PySide2中崩溃。

这些已知错误是吗?预期用法是否有所变化?我想念something in the docs吗?

Win7x64 / Python 3.7.1 / PySide2 5.11.2 (功能不正常/崩溃)


# -*- coding: utf-8 -*-
"""Test QKeySequence equality/matching in PySide2."""
from PySide2.QtCore import Qt
from PySide2.QtGui import QKeySequence

sequence_a = QKeySequence("Shift+Ctrl+Z")
sequence_b = QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_Z)

# string representation
print(sequence_a.toString())
print(sequence_b.toString())

print("-"*20)

# equality/matching
print(sequence_a.matches(sequence_b))
print(sequence_a == sequence_b)

print("-"*20)

# this causes a crash in PySide2 (Process finished with exit code -1073741819 (0xC0000005))
print(QKeySequence(QKeySequence.Redo))

# Ctrl+Shift+Z
# [
# --------------------
# PySide2.QtGui.QKeySequence.SequenceMatch.NoMatch
# False
# --------------------
# Process finished with exit code -1073741819 (0xC0000005)

Win7x64 / Python 2.7.14 / PySide 1.2.4 (正在运行)


# -*- coding: utf-8 -*-
"""Test QKeySequence equality/matching in PySide."""
from PySide.QtCore import Qt
from PySide.QtGui import QKeySequence

sequence_a = QKeySequence("Shift+Ctrl+Z")
sequence_b = QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_Z)

# string representation
print(sequence_a.toString())
print(sequence_b.toString())

print("-"*20)

# equality/matching
print(sequence_a.matches(sequence_b))
print(sequence_a == sequence_b)

print("-"*20)

# this works in PySide
print(QKeySequence(QKeySequence.Redo))

# Ctrl+Shift+Z
# Ctrl+Shift+Z
# --------------------
# PySide.QtGui.QKeySequence.SequenceMatch.ExactMatch
# True
# --------------------
# PySide.QtGui.QKeySequence(67108953, 0, 0, 0)

1 个答案:

答案 0 :(得分:0)

目前,出现了两种可能的解决方法:


  • 错误:QKeySequence(Qt.SHIFT + Qt.CTRL + Qt.Key_Z).toString()-> "["
  • 通过强制优先级修复: QKeySequence(Qt.SHIFT + (Qt.CTRL + Qt.Key_Z)).toString()-> "Ctrl+Shift+Z"
  • 按位或运算符固定: QKeySequence(Qt.ShiftModifier | Qt.CTRL | Qt.Key_Z).toString()-> "Ctrl+Shift+Z"

尽管它们似乎可以解决眼前的直接问题,但由于各种原因,我认为它们并不是那么出色。一般问题仍可能符合PySide2错误!?