我重新实现了QTreeWidget以便能够控制右键菜单。我只想在右键单击某个项目时显示菜单。
当我的应用程序调用nodeMenu.exec_(QtGui.QCursor.pos())时 整个应用程序崩溃。
我在使用python 3.3的Windows7中
这是崩溃:python.exe已停止工作
Windows可以在线检查问题的解决方案。 (见最后的完整错误)
这是代码(已编辑)
from PySide import QtCore, QtGui
import sys
class LTreeWidget(QtGui.QTreeWidget):
def __init__(self, parent=None):
QtGui.QTreeWidget.__init__(self, parent)
self.parent = parent
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.handleHeaderMenu)
def handleHeaderMenu(self, pos):
item = self.itemAt(pos)
if item:
nodeMenu = QtGui.QMenu(self.parent);
deleteNode = QtGui.QAction("Delete node",self.parent)
nodeMenu.addAction(deleteNode)
#self.mapToGlobal(pos))
nodeMenu.exec_(QtGui.QCursor.pos())
else:
return
class Window(QtGui.QWidget):
def __init__(self, parent=None):
# Inherited class constructor
QtGui.QWidget.__init__(self, parent)
# ...
# TreeView Widget
self.tree_widget = LTreeWidget()
self.tree_widget.headerItem().setText(0, "Objects")
item = QtGui.QTreeWidgetItem(self.tree_widget)
item.setText(0, "Cube ")
'''
# TreeView Menu Policy
self.tree_widget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
# TreeView Menu Definition
nodeMenu = QtGui.QMenu(self.tree_widget);
deleteNode = QtGui.QAction("Delete node",nodeMenu)
self.tree_widget.addAction(deleteNode)
'''
# TreeView Scaling Policy
treePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
treePolicy.setHorizontalStretch(1)
self.tree_widget.setSizePolicy(treePolicy)
#...
mainLayout = QtGui.QHBoxLayout()
mainLayout.addWidget(self.tree_widget)
# Set Layout
self.setLayout(mainLayout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
# ...
window.show()
sys.exit(app.exec_())
完成崩溃讯息
Problem signature:
Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 51606889
Fault Module Name: QtGui4.dll
Fault Module Version: 4.8.2.0
Fault Module Timestamp: 4fa6d7c9
Exception Code: c0000005
Exception Offset: 00357388
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 4105
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789