调用输入对话框时的Qt菜单神器
Qt menu artifact when calling input dialog
我正在构建一个 PyQt 应用程序,它应该在 QGraphicsView 上接收鼠标右键单击拖动,绘制一个 "lasso"(一条从拖动原点延伸到鼠标位置的线,并在鼠标位置),然后在松开鼠标时擦除套索图形并为应用程序的下一部分显示输入对话框。
出于某种原因,当我使用鼠标在输入对话框上单击 "Ok" 时,包含套索的 QGraphicsView 上会出现一个菜单工件。菜单工件是一个下拉菜单行,上面写着“(check mark) Exit”。有时它也可能是我的自定义 QGraphicsObjects 之一的上下文菜单 - 但无论出于何种原因,调用对话框然后单击 "Ok" 会导致 QGraphicsView 上发生意外的类似右键单击的事件。
这似乎只发生在方法 return 之前的最后一步是 QInputDialog 时 - 用传递替换它或调用其他方法不会导致工件。如果有人知道导致此问题的原因,我将不胜感激!
这是最简单的代码:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QMainWindow):
# The app main window.
def __init__(self):
super(Window, self).__init__()
# Initialize window UI
self.initUI()
def initUI(self, labelText=None):
# Set user-interface attributes.
# Set up menu-, tool-, status-bars and add associated actions.
self.toolbar = self.addToolBar('Exit')
# Create a menu item to exit the app.
exitAction = QtGui.QAction(QtGui.QIcon('icons/exit.png'), '&Exit', self)
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar.addAction(exitAction)
# Create the main view.
self.viewNetwork = NetworkPortal()
self.viewNetwork.setMinimumWidth(800)
self.viewNetwork.setMinimumHeight(800)
self.setCentralWidget(self.viewNetwork)
self.show()
class NetworkPortal(QtGui.QGraphicsView):
# A view which allows you to see and manipulate a network of nodes.
def __init__(self):
super(NetworkPortal, self).__init__(QtGui.QGraphicsScene())
# Add the CircleThing graphic to the scene.
circleThing = CircleThing()
self.scene().addItem(circleThing)
class CircleThing(QtGui.QGraphicsEllipseItem):
# Defines the graphical object.
def __init__(self):
super(CircleThing, self).__init__(-10, -10, 20, 20)
# Set flags for the graphical object.
self.setFlags(
QtGui.QGraphicsItem.ItemIsSelectable |
QtGui.QGraphicsItem.ItemIsMovable |
QtGui.QGraphicsItem.ItemSendsScenePositionChanges
)
self.dragLine = None
self.dragCircle = None
def mouseMoveEvent(self, event):
# Reimplements mouseMoveEvent to drag out a line which can, on
# mouseReleaseEvent, form a new Relationship or create a new Thing.
# If just beginning a drag,
if self.dragLine == None:
# Create a new lasso line.
self.startPosX = event.scenePos().x()
self.startPosY = event.scenePos().y()
self.dragLine = self.scene().addLine(
self.startPosX,
self.startPosY,
event.scenePos().x(),
event.scenePos().y(),
QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine)
)
# Create a new lasso circle at the location of the drag position.
self.dragCircle = QtGui.QGraphicsEllipseItem(-5, -5, 10, 10)
self.dragCircle.setPos(event.scenePos().x(),
event.scenePos().y())
self.scene().addItem(self.dragCircle)
# If a drag is already in progress,
else:
# Move the lasso line and circle to the drag position.
self.dragLine.setLine(QtCore.QLineF(self.startPosX,
self.startPosY,
event.scenePos().x(),
event.scenePos().y()))
self.dragCircle.setPos(event.scenePos().x(),
event.scenePos().y())
def mouseReleaseEvent(self, event):
# If the line already exists,
if self.dragLine != None:
# If the released button was the right mouse button,
if event.button() == QtCore.Qt.RightButton:
# Clean up the link-drag graphics.
self.scene().removeItem(self.dragLine)
self.dragLine = None
self.scene().removeItem(self.dragCircle)
self.dragCircle = None
# Create the related Thing.
# Display input box querying for name value.
entry, ok = QtGui.QInputDialog.getText(None, 'Enter some info: ',
'Input:', QtGui.QLineEdit.Normal, '')
return
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
newWindow = Window()
sys.exit(app.exec_())
这是我的猜测,但我以前见过这种奇怪的事情。
一些 Qt 小部件对某些类型的事件具有默认行为。我从未使用过 QGraphicsView,但通常右键单击的默认设置是打开一个上下文相关的弹出菜单(根据我的经验,通常是无用的)。您的情况可能会发生这种情况,这可以解释为什么您只有在右键单击时才会看到它。
您可以通过在从 mouseReleaseEvent.
返回之前调用 event.ignore()
来抑制默认的 Qt 行为
没有直接回答此错误的原因,但使用 QTimer.singleShot() 调用对话框(结合 QSignalMapper 以输入参数)是一种功能性解决方法来自发生错误的方法的对话框。感谢@Avaris 的帮助。
我正在构建一个 PyQt 应用程序,它应该在 QGraphicsView 上接收鼠标右键单击拖动,绘制一个 "lasso"(一条从拖动原点延伸到鼠标位置的线,并在鼠标位置),然后在松开鼠标时擦除套索图形并为应用程序的下一部分显示输入对话框。
出于某种原因,当我使用鼠标在输入对话框上单击 "Ok" 时,包含套索的 QGraphicsView 上会出现一个菜单工件。菜单工件是一个下拉菜单行,上面写着“(check mark) Exit”。有时它也可能是我的自定义 QGraphicsObjects 之一的上下文菜单 - 但无论出于何种原因,调用对话框然后单击 "Ok" 会导致 QGraphicsView 上发生意外的类似右键单击的事件。
这似乎只发生在方法 return 之前的最后一步是 QInputDialog 时 - 用传递替换它或调用其他方法不会导致工件。如果有人知道导致此问题的原因,我将不胜感激!
这是最简单的代码:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QMainWindow):
# The app main window.
def __init__(self):
super(Window, self).__init__()
# Initialize window UI
self.initUI()
def initUI(self, labelText=None):
# Set user-interface attributes.
# Set up menu-, tool-, status-bars and add associated actions.
self.toolbar = self.addToolBar('Exit')
# Create a menu item to exit the app.
exitAction = QtGui.QAction(QtGui.QIcon('icons/exit.png'), '&Exit', self)
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar.addAction(exitAction)
# Create the main view.
self.viewNetwork = NetworkPortal()
self.viewNetwork.setMinimumWidth(800)
self.viewNetwork.setMinimumHeight(800)
self.setCentralWidget(self.viewNetwork)
self.show()
class NetworkPortal(QtGui.QGraphicsView):
# A view which allows you to see and manipulate a network of nodes.
def __init__(self):
super(NetworkPortal, self).__init__(QtGui.QGraphicsScene())
# Add the CircleThing graphic to the scene.
circleThing = CircleThing()
self.scene().addItem(circleThing)
class CircleThing(QtGui.QGraphicsEllipseItem):
# Defines the graphical object.
def __init__(self):
super(CircleThing, self).__init__(-10, -10, 20, 20)
# Set flags for the graphical object.
self.setFlags(
QtGui.QGraphicsItem.ItemIsSelectable |
QtGui.QGraphicsItem.ItemIsMovable |
QtGui.QGraphicsItem.ItemSendsScenePositionChanges
)
self.dragLine = None
self.dragCircle = None
def mouseMoveEvent(self, event):
# Reimplements mouseMoveEvent to drag out a line which can, on
# mouseReleaseEvent, form a new Relationship or create a new Thing.
# If just beginning a drag,
if self.dragLine == None:
# Create a new lasso line.
self.startPosX = event.scenePos().x()
self.startPosY = event.scenePos().y()
self.dragLine = self.scene().addLine(
self.startPosX,
self.startPosY,
event.scenePos().x(),
event.scenePos().y(),
QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine)
)
# Create a new lasso circle at the location of the drag position.
self.dragCircle = QtGui.QGraphicsEllipseItem(-5, -5, 10, 10)
self.dragCircle.setPos(event.scenePos().x(),
event.scenePos().y())
self.scene().addItem(self.dragCircle)
# If a drag is already in progress,
else:
# Move the lasso line and circle to the drag position.
self.dragLine.setLine(QtCore.QLineF(self.startPosX,
self.startPosY,
event.scenePos().x(),
event.scenePos().y()))
self.dragCircle.setPos(event.scenePos().x(),
event.scenePos().y())
def mouseReleaseEvent(self, event):
# If the line already exists,
if self.dragLine != None:
# If the released button was the right mouse button,
if event.button() == QtCore.Qt.RightButton:
# Clean up the link-drag graphics.
self.scene().removeItem(self.dragLine)
self.dragLine = None
self.scene().removeItem(self.dragCircle)
self.dragCircle = None
# Create the related Thing.
# Display input box querying for name value.
entry, ok = QtGui.QInputDialog.getText(None, 'Enter some info: ',
'Input:', QtGui.QLineEdit.Normal, '')
return
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
newWindow = Window()
sys.exit(app.exec_())
这是我的猜测,但我以前见过这种奇怪的事情。
一些 Qt 小部件对某些类型的事件具有默认行为。我从未使用过 QGraphicsView,但通常右键单击的默认设置是打开一个上下文相关的弹出菜单(根据我的经验,通常是无用的)。您的情况可能会发生这种情况,这可以解释为什么您只有在右键单击时才会看到它。
您可以通过在从 mouseReleaseEvent.
event.ignore()
来抑制默认的 Qt 行为
没有直接回答此错误的原因,但使用 QTimer.singleShot() 调用对话框(结合 QSignalMapper 以输入参数)是一种功能性解决方法来自发生错误的方法的对话框。感谢@Avaris 的帮助。