【问题标题】:PyQt5 Right Click treeWidget ItemPyQt5 右键单击​​ treeWidget 项
【发布时间】:2017-07-31 20:00:57
【问题描述】:

您好,我正在使用 Python 3.5 和 Pyqt5 开发一个项目,我想在 treeWidget 项上添加一个右键单击,这样我就可以创建一个带有操作的上下文菜单,但我在 PyQt5 中没有找到它。

【问题讨论】:

    标签: python pyqt pyqt5 python-3.5


    【解决方案1】:

    问题已实际解决,这就是您在 QtreeWidget 区域中添加右键单击上下文菜单的方法

     def menuContextuelAlbum(self, event):
        self.menu_contextuelAlb = QtWidgets.QMenu(self.treeWidget)
        ajoutFileAtt = self.menu_contextuelAlb.addAction("Ajouter l'album à la file d'attente")
        action2 = self.menu_contextuelAlb.exec_(self.treeWidget.mapToGlobal(event))
        if action2 is not None:
            if action2 == ajoutFileAtt:
                self.addAlbumlistAtt()
    

    并使用以下命令启动它:

        self.treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)  
        self.treeWidget.customContextMenuRequested.connect(self.menuContextuelAlbum)  
        self.actionOuvrir.triggered.connect(self.menu)  
    

    【讨论】:

      【解决方案2】:

      您需要使用自己的mousePressEvent 创建自己的QTreeWidget。 在您的mousePressEvent 中检查事件类型是否为右键单击,如果是,请执行您想要的任何操作。然后,当您在树中添加小部件时,确保添加的是您的类,而不是 QTreeWidget

      类似这样的:

      from PyQt4 import QtCore, QtGui
      from PyQt4.QtGui import QTreeWidgetItem
      import sys
      
      class MyTreeWidget(QtGui.QTreeWidget):
          def __init__(self, parent = None):
              QtGui.QTreeWidget.__init__(self, parent)
          def mousePressEvent (self, event):
              print("child clicked ! ")
              if event.button() == QtCore.Qt.RightButton:
                  print("right click !")
              QtGui.QTreeWidget.mousePressEvent(self, event)
      
      def main():
          app = QtGui.QApplication(sys.argv)
          QtGui.qApp = app
          pointListBox = MyTreeWidget()
          root = QTreeWidgetItem(pointListBox, ["root"])
          A = QTreeWidgetItem(root, ["A"])
          barA = QTreeWidgetItem(A, ["bar", "i", "ii"])
          bazA = QTreeWidgetItem(A, ["baz", "a", "b"])
      
          pointListBox.show()
      
          sys.exit(app.exec_())
      
      if __name__ == '__main__':
          main()
      

      【讨论】:

      • 感谢您的快速回答,我如何在文件中单独尝试? if __name__ == "__main__": import sys app = QApplication(sys.argv) window = MytreeWidgetItem() window.show() sys.exit(app.exec_())
      • 你问我的代码有很多错误是对的。我将通过一个工作示例来解决这个问题。
      • 谢谢,但主题是关于 PyQt5,因为没有太多关于它的文档,这有点烦人,而且 PyQt5 并没有使用与 PyQt4 完全相同的对象。不过谢谢你的回答
      • 实际上,我发现将 PyQt4 转换为 pyqt5 相当简单,没有很多问题。有时我所要做的就是将 PyQt4 引用换成 PyQt5 引用,所以上面的答案可能通过一些调整就可行
      【解决方案3】:

      发布更新的答案。这对我有用:

      class MainWindow_W():
          def __init__(self):
              self.app = QtWidgets.QApplication(sys.argv)
              self.MainWindow = QtWidgets.QMainWindow()
              self.ui = Ui_MainWindow()
              self.ui.setupUi(self.MainWindow)
      
      
      
              # Connect the contextmenu
              self.ui.treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
              self.ui.treeWidget.customContextMenuRequested.connect(self.menuContextTree)
      
      
          def menuContextTree(self, point):
              # Infos about the node selected.
              index = self.ui.treeWidget.indexAt(point)
      
              if not index.isValid():
                  return
      
              item = self.ui.treeWidget.itemAt(point)
              name = item.text(0)  # The text of the node.
      
              # We build the menu.
              menu = QtWidgets.QMenu()
              action = menu.addAction("Souris au-dessus de")
              action = menu.addAction(name)
              menu.addSeparator()
              action_1 = menu.addAction("Choix 1")
              action_2 = menu.addAction("Choix 2")
              action_3 = menu.addAction("Choix 3")
      
      
              menu.exec_(self.ui.treeWidget.mapToGlobal(point))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多