您将在下面的图片中看到我想要这样做,这样用户就可以点击劳拉(这是一个QMenu),它就会发生火灾和事件。就像常规行动一样。
我根据下面的层次结构动态创建子菜单。但是,除了“孩子们”之外,我希望所有子菜单都可以点击。
奇怪的是' hover'事件触发但不触发事件?为什么会这样?
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore
# Main Widget
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):
def __init__(self,):
super(ExampleWidget, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.setWindowTitle("Example")
# context menu
self.main_menu = QtGui.QMenu()
self.sub_menu = QtGui.QMenu("Kids")
self.main_menu.addMenu(self.sub_menu)
tree = [
{
"parent" : "Chris",
"children" : [
{
"parent" : "Doug",
"children" : []
},
{
"parent" : "Michelle",
"children" : [
{
"parent" : "Susan",
"children" : []
}
]
}
]
},
{
"parent" : "Laura",
"children" : [
{
"parent" : "Michael",
"children" : []
}
]
}
]
self.build_sub_menu( self.sub_menu, tree )
# widgets
self.factionsList = QtGui.QListWidget()
# signal
self.factionsList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.factionsList.customContextMenuRequested.connect(self.on_context_menu_factions)
# layout
self.mainLayout = QtGui.QGridLayout(self)
self.mainLayout.addWidget(self.factionsList, 1, 0)
self.show()
def add_menu_item(self, menu, branch):
parent = branch["parent"]
sub_menu = QtGui.QMenu(parent)
item = menu.addMenu(sub_menu)
item.triggered.connect(self.menu_action)
item.hovered.connect(self.menu_action)
print item
for c in branch["children"]:
self.add_menu_item( sub_menu, c)
def build_sub_menu(self, menu, tree):
for branch in tree:
self.add_menu_item(menu, branch)
def menu_action(self):
print "Event"
print self.sender().text()
def on_context_menu_factions(self, pos):
self.main_menu.exec_( QtGui.QCursor.pos() )
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = ExampleWidget()
res = app.exec_()
sys.exit(res)
答案 0 :(得分:0)
import threading
import time
exitFlag = 0
class process (threading.Thread):
def __init__(self, processId = None , processName = None , initialTime = None , longTime = None ):
threading.Thread.__init__(self)
self.processId = processId
self.processName = processName
self.initialTime = initialTime
self.longTime = longTime
def run(self):
print "Starting " + self.processName
print_time(self.processName, self.longTime, 5)
print "Exiting " + self.processName
def __str__(self):
threading.Thread.__str__(self)
return ('[' + str(self.initialTime) + ' ' + str(self.longTime) +']')
###############################
####### my problem area #######
###############################
def __cmp__(self, other):
if (self.initialTime > other.initialTime):
return -1
elif (self.initialTime < other.initialTime):
return 1
elif ((self.initialTime == other.initialTime)):
if (self.longTime > other.longTime):
return -1
elif (self.longTime < other.longTime):
return 1
else :
return 0
else:
return 0
###############################
####### my problem area #######
###############################
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
文档说明QMenu
信号:
注意:此信号是针对层次结构中的主父菜单发出的。因此,只需要将父菜单连接到插槽;子菜单无需连接
我想这是因为它是一种UI惯例,点击子菜单什么都不做:它们在悬停时打开,除了打开之外不能做任何事情。您想要的行为是不寻常的,如果您需要triggered
信号,我认为需要对QMenu
进行子分类。
您可以尝试连接triggered
信号。
另一种选择是添加某种“。”对表示父子菜单操作的每个子菜单的操作。