<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # PyQt5 拖放 > 原文: [http://zetcode.com/gui/pyqt5/dragdrop/](http://zetcode.com/gui/pyqt5/dragdrop/) 在 PyQt5 教程的這一部分中,我們將討論拖放操作。 在計算機圖形用戶界面中,拖放是單擊虛擬對象并將其拖動到其他位置或另一個虛擬對象上的動作(或支持以下動作)。 通常,它可用于調用多種動作,或在兩個抽象對象之間創建各種類型的關聯。 拖放是圖形用戶界面的一部分。 拖放操作使用戶可以直觀地執行復雜的操作。 通常,我們可以拖放兩件事:數據或某些圖形對象。 如果將圖像從一個應用拖到另一個應用,則會拖放二進制數據。 如果我們在 Firefox 中拖動選項卡并將其移動到另一個位置,則將拖放圖形組件。 ## `QDrag` `QDrag`支持基于 MIME 的拖放數據傳輸。 它處理拖放操作的大多數細節。 傳輸的數據包含在`QMimeData`對象中。 ## 簡單的拖放 在第一個示例中,我們有一個`QLineEdit`和一個`QPushButton`。 我們將純文本從行編輯小部件中拖放到按鈕小部件上。 按鈕的標簽將更改。 `simpledragdrop.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This is a simple drag and drop example. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication) import sys class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore() def dropEvent(self, e): self.setText(e.mimeData().text()) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): edit = QLineEdit('', self) edit.setDragEnabled(True) edit.move(30, 65) button = Button("Button", self) button.move(190, 65) self.setWindowTitle('Simple drag and drop') self.setGeometry(300, 300, 300, 150) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() app.exec_() ``` 該示例展示了一個簡單的拖動&放下操作。 ```py class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) ... ``` 為了在`QPushButton`小部件上放置文本,我們必須重新實現一些方法。 因此,我們創建了自己的`Button`類,該類將從`QPushButton`類繼承。 ```py self.setAcceptDrops(True) ``` 我們使用`setAcceptDrops()`為小部件啟用放置事件。 ```py def dragEnterEvent(self, e): if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore() ``` 首先,我們重新實現`dragEnterEvent()`方法。 我們告知我們接受的數據類型。 在我們的情況下,它是純文本。 ```py def dropEvent(self, e): self.setText(e.mimeData().text()) ``` 通過重新實現`dropEvent()`方法,我們定義了放置事件發生了什么。 在這里,我們更改按鈕小部件的文本。 ```py edit = QLineEdit('', self) edit.setDragEnabled(True) ``` `QLineEdit`小部件具有對拖動操作的內置支持。 我們需要做的就是調用`setDragEnabled()`方法來激活它。 ![Simple drag and drop](https://img.kancloud.cn/48/76/4876f249186d67e51f0697c882638f32_302x175.jpg) 圖:簡單 drag and drop ## 拖放按鈕小部件 在下面的示例中,我們將演示如何拖放按鈕小部件。 `dragbutton.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this program, we can press on a button with a left mouse click or drag and drop the button with the right mouse click. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import QPushButton, QWidget, QApplication from PyQt5.QtCore import Qt, QMimeData from PyQt5.QtGui import QDrag import sys class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) def mouseMoveEvent(self, e): if e.buttons() != Qt.RightButton: return mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.exec_(Qt.MoveAction) def mousePressEvent(self, e): super().mousePressEvent(e) if e.button() == Qt.LeftButton: print('press') class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setAcceptDrops(True) self.button = Button('Button', self) self.button.move(100, 65) self.setWindowTitle('Click or Move') self.setGeometry(300, 300, 280, 150) def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): position = e.pos() self.button.move(position) e.setDropAction(Qt.MoveAction) e.accept() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() app.exec_() ``` 在我們的代碼示例中,窗口上有一個`QPushButton`。 如果我們用鼠標左鍵單擊該按鈕,則會在控制臺上顯示`'press'`消息。 通過右鍵單擊并移動按鈕,我們可以對按鈕小部件執行拖放操作。 ```py class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) ``` 我們創建一個從`QPushButton`派生的`Button`類。 我們還重新實現了`QPushButton`的兩種方法:`mouseMoveEvent()`和`mousePressEvent()`。 `mouseMoveEvent()`方法是拖放操作開始的地方。 ```py if e.buttons() != Qt.RightButton: return ``` 在這里,我們決定只能用鼠標右鍵執行拖放操作。 鼠標左鍵保留用于單擊該按鈕。 ```py mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) ``` `QDrag`對象已創建。 該類提供對基于 MIME 的拖放數據傳輸的支持。 ```py dropAction = drag.exec_(Qt.MoveAction) ``` 拖動對象的`exec_()`方法開始拖放操作。 ```py def mousePressEvent(self, e): super().mousePressEvent(e) if e.button() == Qt.LeftButton: print('press') ``` 如果我們用鼠標左鍵單擊按鈕,我們將在控制臺上打印`'press'`。 注意,我們也在父對象上調用了`mousePressEvent()`方法。 否則,我們將看不到按鈕被按下。 ```py position = e.pos() self.button.move(position) ``` 在`dropEvent()`方法中,我們指定釋放鼠標按鈕并完成放置操作后發生的情況。 在本例中,我們找出當前鼠標指針的位置并相應地移動按鈕。 ```py e.setDropAction(Qt.MoveAction) e.accept() ``` 我們用`setDropAction()`指定放置動作的類型。 在我們的情況下,這是一個動作。 PyQt5 教程的這一部分專門用于拖放操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看