【问题标题】:PyQt5 - Drag and Drop in QDialogPyQt5 - 在 QDialog 中拖放
【发布时间】:2018-06-17 13:01:24
【问题描述】:

我有一个主要的 pyqt5 应用程序。

单击按钮时我想调用 QDialog 并在提交 QDialog 时想在主应用程序中捕获 QDialog 的结果。

下面是一个 pyqt 模块,我喜欢将它添加到 QDialog,这可能吗,并且想在主应用程序中也捕获结果?

我不知道从哪里开始。

import sys
from PyQt5.QtCore import QSize
from PyQt5 import QtWidgets, QtCore,QtGui
from PyQt5.QtGui import QColor, QDrag, QPainter, QPixmap
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtWidgets
from win32api import GetSystemMetrics
from PyQt5.QtGui import QImage, QPalette, QBrush

w=GetSystemMetrics(0)/1366
h=GetSystemMetrics(1)/768

class Button(QtWidgets.QPushButton):
    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return

        # write the relative cursor position to mime data
        mimeData = QtCore.QMimeData()
        # simple string with 'x,y'
        mimeData.setText('%d,%d' % (e.x(), e.y()))

        pixmap = QtWidgets.QWidget.grab(self)

        # below makes the pixmap half transparent
        painter = QtGui.QPainter(pixmap)
        painter.setCompositionMode(painter.CompositionMode_DestinationIn)
        painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
        painter.end()

        # make a QDrag
        drag = QtGui.QDrag(self)
        # put our MimeData
        drag.setMimeData(mimeData)
        # set its Pixmap
        drag.setPixmap(pixmap)
        # shift the Pixmap so that it coincides with the cursor position
        drag.setHotSpot(e.pos())

        # start the drag operation
        # exec_ will return the accepted action from dropEvent
        if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
            print('moved')
        else:
            print('copied')

class Example(QtWidgets.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setAcceptDrops(True)

        button = Button('Draggable Text', self)
        button.setStyleSheet("font-size: 15px;background-color: rgb(255,255,255); margin:5px; border:1px solid rgb(255, 255, 255); ")
        button.move(100, 65)
        self.buttons = [button]
        self.setWindowTitle('Copy or Move')
        self.setFixedSize(960, 515)

        oImage = QImage(r"C:\Users\vaas\Desktop\background.jpg")
        sImage = oImage.scaled(QSize(960*w/1,515*h/1))                   # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(QPalette.ColorRole(10), QBrush(sImage))                     # 10 = Windowrole
        self.setPalette(palette)

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        # get the relative position from the mime data
        mime = e.mimeData().text()
        x, y = map(int, mime.split(','))

        if e.keyboardModifiers() & QtCore.Qt.ShiftModifier:
            # copy
            # so create a new button
            button = Button('Button', self)
            # move it to the position adjusted with the cursor position at drag
            button.move(e.pos()-QtCore.QPoint(x, y))
            # show it
            button.show()
            # store it
            self.buttons.append(button)
            # set the drop action as Copy
            e.setDropAction(QtCore.Qt.CopyAction)
        else:
            # move
            # so move the dragged button (i.e. event.source())
            print('mine°',e.pos())
            e.source().move(e.pos()-QtCore.QPoint(x, y))
            # set the drop action as Move
            e.setDropAction(QtCore.Qt.MoveAction)
        # tell the QDrag we accepted it
        e.accept()

def main():
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

【问题讨论】:

  • 您可以更好地解释我,我看到按钮被拖动了,您的代码中缺少什么?

标签: python pyqt5


【解决方案1】:

http://doc.qt.io/qt-5/dnd.html开头

试一试下面的例子:

import sys
from PyQt5           import QtWidgets, QtCore, QtGui
from PyQt5.QtCore    import QSize
from PyQt5.QtGui     import (QColor, QDrag, QPainter, QPixmap, 
                             QImage, QPalette, QBrush)
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout
from win32api import GetSystemMetrics


w=GetSystemMetrics(0)/1366
h=GetSystemMetrics(1)/768

### +++++++++++++++++++++++++++++
class Window2(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        # To be able to receive media dropped on a widget, call setAcceptDrops(true) 
        # for the widget, and reimplement the dragEnterEvent() and 
        # dropEvent() event handler functions.
        self.setAcceptDrops(True)

        self.setGeometry(QtCore.QRect(20, 50, 180, 600))
        self.lay = QVBoxLayout()
        self.lay.addWidget(QtWidgets.QPushButton('Button1'))
        self.lay.addWidget(QtWidgets.QPushButton('Button2'))
        self.lay.addWidget(QtWidgets.QPushButton('Button3'))
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor('blue'))
        self.setPalette(palette)
        self.setLayout(self.lay)
        self.textLabel = ''

    # The dragEnterEvent() function is typically used to inform Qt about the types 
    # of data that the widget accepts. You must reimplement this function 
    # if you want to receive either QDragMoveEvent or QDropEvent 
    # in your reimplementations of dragMoveEvent() and dropEvent().
    def dragEnterEvent(self, e):
        e.accept()

    # The dropEvent() is used to unpack dropped data and 
    # handle it in way that is suitable for your application.
    def dropEvent(self, e):
        #self.textLabel = 'Drag and Drop. QLabel. e.mimeData().text() -> `{}`'.format(e.mimeData().text())
        self.lay.addWidget(QtWidgets.QLabel('Drag and Drop. QLabel. e.mimeData().text() -> `{}`'\
                                            .format(e.mimeData().text())))
        e.accept()


class Button(QtWidgets.QPushButton):
    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return

        # write the relative cursor position to mime data
        mimeData = QtCore.QMimeData()
        # simple string with 'x,y'
        mimeData.setText('%d,%d' % (e.x(), e.y()))

        pixmap = QtWidgets.QWidget.grab(self)

        # below makes the pixmap half transparent
        painter = QtGui.QPainter(pixmap)
        painter.setCompositionMode(painter.CompositionMode_DestinationIn)
        painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
        painter.end()

        # make a QDrag
        drag = QtGui.QDrag(self)
        # put our MimeData
        drag.setMimeData(mimeData)
        # set its Pixmap
        drag.setPixmap(pixmap)
        # shift the Pixmap so that it coincides with the cursor position
        drag.setHotSpot(e.pos())

        # start the drag operation
        # exec_ will return the accepted action from dropEvent
        if drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
            print('moved')
        else:
            print('copied')

class Example(QtWidgets.QWidget):
    def __init__(self):
        super(Example, self).__init__()

        self.w2 = Window2()                 # +++
        self.w2.show()                      # +++        

        self.initUI()

    def initUI(self):
        self.setAcceptDrops(True)

        button = Button('Draggable Text', self)
        button.setStyleSheet("font-size: 15px;background-color: rgb(255,255,255); margin:5px; border:1px solid rgb(255, 255, 255); ")
        button.move(100, 65)
        self.buttons = [button]
        self.setWindowTitle('Copy or Move')
        self.setFixedSize(960, 515)

        oImage = QImage(r"C:\Users\vaas\Desktop\background.jpg")
        #oImage = QImage(r"logo.png")
        sImage = oImage.scaled(QSize(960*w/1,515*h/1))              # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(QPalette.ColorRole(10), QBrush(sImage))    # 10 = Windowrole
        self.setPalette(palette)

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        # get the relative position from the mime data
        mime = e.mimeData().text()
        x, y = map(int, mime.split(','))

        if e.keyboardModifiers() & QtCore.Qt.ShiftModifier:
            # copy
            # so create a new button
            button = Button('Button', self)
            # move it to the position adjusted with the cursor position at drag
            button.move(e.pos()-QtCore.QPoint(x, y))
            # show it
            button.show()
            # store it
            self.buttons.append(button)
            # set the drop action as Copy
            e.setDropAction(QtCore.Qt.CopyAction)
        else:
            # move
            # so move the dragged button (i.e. event.source())
            print('mine°',e.pos())
            e.source().move(e.pos()-QtCore.QPoint(x, y))
            # set the drop action as Move
            e.setDropAction(QtCore.Qt.MoveAction)
        # tell the QDrag we accepted it
        e.accept()

def main():
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 2017-05-27
    • 2018-10-18
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多