久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='c0bS7'></bdo><ul id='c0bS7'></ul>

  • <tfoot id='c0bS7'></tfoot>

    <small id='c0bS7'></small><noframes id='c0bS7'>

    <legend id='c0bS7'><style id='c0bS7'><dir id='c0bS7'><q id='c0bS7'></q></dir></style></legend>

      <i id='c0bS7'><tr id='c0bS7'><dt id='c0bS7'><q id='c0bS7'><span id='c0bS7'><b id='c0bS7'><form id='c0bS7'><ins id='c0bS7'></ins><ul id='c0bS7'></ul><sub id='c0bS7'></sub></form><legend id='c0bS7'></legend><bdo id='c0bS7'><pre id='c0bS7'><center id='c0bS7'></center></pre></bdo></b><th id='c0bS7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='c0bS7'><tfoot id='c0bS7'></tfoot><dl id='c0bS7'><fieldset id='c0bS7'></fieldset></dl></div>

        帶有上下文菜單的 QTreeWidget,無法獲取正確的項

        QTreeWidget with contextmenu, can#39;t get correct item(帶有上下文菜單的 QTreeWidget,無法獲取正確的項目)
            <tfoot id='s3WVe'></tfoot>

          1. <legend id='s3WVe'><style id='s3WVe'><dir id='s3WVe'><q id='s3WVe'></q></dir></style></legend>

              <bdo id='s3WVe'></bdo><ul id='s3WVe'></ul>
              <i id='s3WVe'><tr id='s3WVe'><dt id='s3WVe'><q id='s3WVe'><span id='s3WVe'><b id='s3WVe'><form id='s3WVe'><ins id='s3WVe'></ins><ul id='s3WVe'></ul><sub id='s3WVe'></sub></form><legend id='s3WVe'></legend><bdo id='s3WVe'><pre id='s3WVe'><center id='s3WVe'></center></pre></bdo></b><th id='s3WVe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='s3WVe'><tfoot id='s3WVe'></tfoot><dl id='s3WVe'><fieldset id='s3WVe'></fieldset></dl></div>
            • <small id='s3WVe'></small><noframes id='s3WVe'>

                    <tbody id='s3WVe'></tbody>
                  本文介紹了帶有上下文菜單的 QTreeWidget,無法獲取正確的項目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有以下代碼來創建一個 QTreeWidget 和一個包含 2 個操作的上下文菜單.

                  I have the following code to create a QTreeWidget and a contextmenu with 2 actions.

                  import sys
                  from PyQt5 import QtCore, QtWidgets
                  
                  class Dialog(QtWidgets.QDialog):
                      def __init__(self, parent=None):
                          super(Dialog, self).__init__()
                  
                          self.tw = QtWidgets.QTreeWidget()
                          self.tw.setHeaderLabels(['Name', 'Cost ($)'])
                          cg = QtWidgets.QTreeWidgetItem(['carrots', '0.99'])
                          c1 = QtWidgets.QTreeWidgetItem(['carrot', '0.33'])
                          self.tw.addTopLevelItem(cg)
                          self.tw.addTopLevelItem(c1)
                          self.tw.installEventFilter(self)
                          layout = QtWidgets.QVBoxLayout(self)
                          layout.addWidget(self.tw)
                  
                      def eventFilter(self, source: QtWidgets.QTreeWidget, event):
                          if (event.type() == QtCore.QEvent.ContextMenu and
                              source is self.tw):
                              menu = QtWidgets.QMenu()
                              AAction = QtWidgets.QAction("AAAAA")
                              AAction.triggered.connect(lambda :self.a(source.itemAt(event.pos())))
                              BAction = QtWidgets.QAction("BBBBBB")
                              BAction.triggered.connect(lambda :self.b(source, event))
                              menu.addAction(AAction)
                              menu.addAction(BAction)
                              menu.exec_(event.globalPos())
                              return True
                          return super(Dialog, self).eventFilter(source, event)
                  
                      def a(self, item):
                          if item is None:
                              return
                          print("A: {}".format([item.text(i) for i in range(self.tw.columnCount())]))
                      def b(self, source, event):
                          item = source.itemAt(event.pos())
                          if item is None:
                              return
                          print("B: {}".format([item.text(i) for i in range(source.columnCount())]))
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      window = Dialog()
                      window.setGeometry(600, 100, 300, 200)
                      window.show()
                      sys.exit(app.exec_())
                  

                  當打開標題中的上下文菜單并單擊其中一個操作時,它會打印胡蘿卜或胡蘿卜,具體取決于我在上下文菜單中單擊的位置.但是我把右鍵事件的位置給了函數.

                  When opening the contextmenu in the header and clicking on one of the actions it prints either carrot or carrots, depending on where in the contextmenu I click. But I give the position of right click event to the functions.

                  那么為什么會發生這種情況,我該怎么做才能阻止它?

                  So why is this happening and what can I do to stop it?

                  推薦答案

                  你的代碼有2個錯誤:

                  • 主要錯誤是 itemAt() 方法使用相對于 viewport() 的坐標,而不是相對于視圖(QTreeWidget),所以你會得到不正確的項目(標題占用空間相對于 QTreeWidget 和 viewport() 的位置有一個偏移量).

                  • The main error is that the itemAt() method uses the coordinates with respect to the viewport() and not with respect to the view (the QTreeWidget) so you will get incorrect items (the header occupies a space making the positions with respect to the QTreeWidget and the viewport() have an offset).

                  你不應該阻塞事件循環,例如阻塞 eventFilter 你可能阻塞了其他會導致難以調試的錯誤的事件,在這種情況下最好使用信號.

                  You should not block the eventloop, for example blocking the eventFilter you may be blocking other events that would cause errors that are difficult to debug, in this case it is better to use a signal.

                  class Dialog(QtWidgets.QDialog):
                      rightClicked = QtCore.pyqtSignal(QtCore.QPoint)
                  
                      def __init__(self, parent=None):
                          super(Dialog, self).__init__()
                  
                          self.tw = QtWidgets.QTreeWidget()
                          self.tw.setHeaderLabels(["Name", "Cost ($)"])
                          cg = QtWidgets.QTreeWidgetItem(["carrots", "0.99"])
                          c1 = QtWidgets.QTreeWidgetItem(["carrot", "0.33"])
                          self.tw.addTopLevelItem(cg)
                          self.tw.addTopLevelItem(c1)
                  
                          self.tw.viewport().installEventFilter(self)
                  
                          layout = QtWidgets.QVBoxLayout(self)
                          layout.addWidget(self.tw)
                  
                          self.rightClicked.connect(self.handle_rightClicked)
                  
                      def eventFilter(self, source: QtWidgets.QTreeWidget, event):
                          if event.type() == QtCore.QEvent.ContextMenu and source is self.tw.viewport():
                              self.rightClicked.emit(event.pos())
                              return True
                  
                          return super(Dialog, self).eventFilter(source, event)
                  
                      def handle_rightClicked(self, pos):
                          item = self.tw.itemAt(pos)
                          if item is None:
                              return
                          menu = QtWidgets.QMenu()
                          print_action = QtWidgets.QAction("Print")
                          print_action.triggered.connect(lambda checked, item=item: self.print_item(item))
                          menu.addAction(print_action)
                          menu.exec_(self.tw.viewport().mapToGlobal(pos))
                  
                      def print_item(self, item):
                          if item is None:
                              return
                          texts = []
                          for i in range(item.columnCount()):
                              text = item.text(i)
                              texts.append(text)
                  
                          print("B: {}".format(",".join(texts)))
                  

                  雖然沒有必要使用 eventFilter 來處理 contextmenu,因為更簡單的解決方案是將 QTreeWidget 的 contextMenuPolicy 設置為 Qt::CustomContextMenu 并使用 customContextMenuRequested 信號:

                  Although it is unnecessary that you use an eventFilter to handle the contextmenu since a simpler solution is to set the contextMenuPolicy of the QTreeWidget to Qt::CustomContextMenu and use the customContextMenuRequested signal:

                  class Dialog(QtWidgets.QDialog):
                      def __init__(self, parent=None):
                          super(Dialog, self).__init__()
                  
                          self.tw = QtWidgets.QTreeWidget()
                          self.tw.setHeaderLabels(["Name", "Cost ($)"])
                          cg = QtWidgets.QTreeWidgetItem(["carrots", "0.99"])
                          c1 = QtWidgets.QTreeWidgetItem(["carrot", "0.33"])
                          self.tw.addTopLevelItem(cg)
                          self.tw.addTopLevelItem(c1)
                  
                          self.tw.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
                          self.tw.customContextMenuRequested.connect(self.handle_rightClicked)
                  
                          layout = QtWidgets.QVBoxLayout(self)
                          layout.addWidget(self.tw)
                  
                      def handle_rightClicked(self, pos):
                          item = self.tw.itemAt(pos)
                          if item is None:
                              return
                          menu = QtWidgets.QMenu()
                          print_action = QtWidgets.QAction("Print")
                          print_action.triggered.connect(lambda checked, item=item: self.print_item(item))
                          menu.addAction(print_action)
                          menu.exec_(self.tw.viewport().mapToGlobal(pos))
                  
                      def print_item(self, item):
                          if item is None:
                              return
                          texts = []
                          for i in range(item.columnCount()):
                              text = item.text(i)
                              texts.append(text)
                  
                          print("B: {}".format(",".join(texts)))
                  

                  這篇關于帶有上下文菜單的 QTreeWidget,無法獲取正確的項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  <tfoot id='I6dig'></tfoot>

                          • <bdo id='I6dig'></bdo><ul id='I6dig'></ul>
                              <tbody id='I6dig'></tbody>
                            <legend id='I6dig'><style id='I6dig'><dir id='I6dig'><q id='I6dig'></q></dir></style></legend>
                          • <i id='I6dig'><tr id='I6dig'><dt id='I6dig'><q id='I6dig'><span id='I6dig'><b id='I6dig'><form id='I6dig'><ins id='I6dig'></ins><ul id='I6dig'></ul><sub id='I6dig'></sub></form><legend id='I6dig'></legend><bdo id='I6dig'><pre id='I6dig'><center id='I6dig'></center></pre></bdo></b><th id='I6dig'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='I6dig'><tfoot id='I6dig'></tfoot><dl id='I6dig'><fieldset id='I6dig'></fieldset></dl></div>

                            <small id='I6dig'></small><noframes id='I6dig'>

                            主站蜘蛛池模板: 精品视频www | 久产久精国产品 | 国产精品美女久久久久aⅴ国产馆 | 国产精品无码久久久久 | 九九av| 天天色天天色 | 国产欧美精品一区二区色综合朱莉 | 91免费看片| xxx国产精品视频 | 亚洲高清av| 欧美一区二区在线播放 | 亚洲色图插插插 | 正在播放国产精品 | 国产精品视频在线播放 | 久久久久久国产精品 | 成人午夜精品 | 国产免费一区二区 | 日韩1区 | 91免费在线看 | 精品视频一二区 | 国产精品久久久亚洲 | 毛片大全| 日韩免费毛片视频 | 欧美日韩精品一区 | 午夜手机在线视频 | 亚洲免费网站 | 亚洲精品一区二区三区四区高清 | 国内精品久久精品 | 91精品国产一区二区三区 | 欧美精品中文 | 免费一二区| 成人依人 | 欧产日产国产精品99 | 三级av在线 | 日本成人福利 | 在线男人天堂 | 欧美国产日本一区 | 午夜精品一区二区三区在线视频 | 日韩免费网 | 一级黄色片在线看 | 精品福利视频一区二区三区 |