我正在构建一个PyQt应用程序,该应用程序应该在QGraphicsView上接收鼠标右键单击拖动,绘制一个"套索" (从拖动原点延伸到鼠标位置的线和鼠标位置的圆圈),然后,在鼠标释放时,擦除套索图形并显示应用程序下一部分的输入对话框。
出于某种原因,当我使用鼠标点击" Ok"在输入对话框中,QGraphicsView上出现一个包含套索的菜单工件。菜单工件是一个下拉菜单行,显示"(复选标记)退出"。有时它也可能是我的一个自定义QGraphicsObjects的上下文菜单 - 但无论出于何种原因,调用对话框然后单击" Ok"导致QGraphicsView上出现意外的类似右键单击的事件。
这种情况似乎只发生在返回方法之前的最后一步是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_())
答案 0 :(得分:0)
这是我的猜测,但我之前见过这种奇怪的事情。
某些Qt小部件在某些类型的事件上具有默认行为。我从未使用过QGraphicsView,但右键单击的默认设置通常是打开一个上下文相关的弹出菜单(根据我的经验,通常没用)。在您的情况下可能会发生这种情况,这可以解释为什么只有在右键单击时才能看到这一点。
您可以在从event.ignore()
mouseReleaseEvent.
来禁止默认的Qt行为
答案 1 :(得分:0)
没有直接回答此错误的原因,但使用QTimer.singleShot()调用对话框(与QSignalMapper结合以输入参数)是一个功能性的解决方法,将对话框与发生错误的方法。感谢@Avaris这个。