代码之家  ›  专栏  ›  技术社区  ›  Eric O. Lebigot

Qt:将上下文菜单添加到中央小部件的最佳方法?

  •  1
  • Eric O. Lebigot  · 技术社区  · 14 年前

    我不明白书中为什么 用python和qt快速进行GUI编程 ,通过调用 addActions() 在主窗口上 ( self )如是(P._180):

    self.addActions(self.imageLabel,
                    (editInvertAction, …))
    

    在哪里? 自己 是一个 QMainWindow imageLabel 是一个 QLabel 设置为中心小部件

    # Added actions will be put in a context menu:
    self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)
    self.setCentralWidget(self.imageLabel)
    

    现在,为什么主窗口会以某种方式(通过 self.addActions() )到中央小部件的上下文菜单?打电话不够吗 AdActudio() 直接在中央小部件上?实际上,以下内容确实创建了一个上下文菜单:

    self.imageLabel.addActions((editInvertAction, …))
    

    为什么这本书不以这种方式作为上下文菜单的例子?这不等于更复杂吗 self.addActions(…) 形式?

    聚苯乙烯 我甚至看到 documentation for QMainWindow.addActions() 甚至没有提到任何第一个论点( self.imageLabel 以上)!我完全搞不懂这本书为什么用上面的第一个片段而不是最后一个“帮助”!:)

    1 回复  |  直到 7 年前
        1
  •  1
  •   iksaif    14 年前

    在qmainwindow上使用self.addaction()允许所有qmainwindow子项(docks、statusbar、toolbar、menubar…)使用这些操作,而不仅仅是中心小部件。

    但是获得细粒度上下文菜单控件的最佳方法是使用CustomContextMenuQuested信号( http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#customContextMenuRequested )