当两个按钮使用相同的功能时,如何知道正在使用哪个按钮

时间:2019-05-02 10:52:51

标签: python pyqt

我的程序有:

两个按钮,可以用鼠标移动并相互链接


我使用按钮btn1btn2 写在视图中

我的问题是: 它们调用相同的函数Action1Action2。我想回传我在致电Action2时使用的按钮位置。我该怎么办?

我的成就是“使用Action2打印按钮的位置”

这是我的代码:

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag


class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.view = View(self)
        self.button = QPushButton('Clear View', self)
        self.button.clicked.connect(self.handleClearView)
        layout = QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.button)


    def handleClearView(self):
        self.view.scene().clear()


class DragButton(QPushButton):
    def __init__(self, title, parent=None):
        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)


class View(QGraphicsView):
    def __init__(self, parent):
        QGraphicsView.__init__(self, parent)
        self.setScene(QGraphicsScene(self))
        self.setAcceptDrops(True)
        self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
        self.btn1=DragButton('Test1', self)
        self.btn2=DragButton('Test2', self)
        self.btn2.setGeometry(230, 80, 100, 30)

        self.menu=QMenu()
        self.menu1=QMenu()
        self.menu.addAction('use', self.Action1)
        self.menu.addAction('sendback', self.Action2)
        self.menu.addAction('delete',self.btn1.deleteLater)

        self.menu1.addAction('use', self.Action1)
        self.menu1.addAction('sendback', self.Action2)
        self.menu1.addAction('delete',self.btn2.deleteLater)

        self.btn1.setMenu(self.menu)
        self.btn2.setMenu(self.menu1)

    def Action2(self):
        print("sendback position")


    def Action1(self):
        print("~")

    def clearScene(self):
        self.scene().clear()

    def dragEnterEvent(self, e):
        if e.source() in [self.btn1, self.btn2]:
            self.clearScene()
        e.accept()

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

    def dropEvent(self, e):
        btn = e.source()
        otherBtn = self.btn2 if btn == self.btn1 else self.btn1
        position = e.pos()
        btn.move(position)

        start = QtCore.QPointF(self.mapToScene(btn.pos()))
        end = QtCore.QPointF(self.mapToScene(otherBtn.pos()))

        self.scene().addItem(
            QGraphicsLineItem(QtCore.QLineF(start, end)))

        e.setDropAction(Qt.MoveAction)
        e.accept()


if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

直接在您的DragButton类中移动菜单和操作。

class DragButton(QPushButton):
    def __init__(self, title, parent=None):
        super().__init__(title, parent)
        self.menu=QMenu()
        self.menu.addAction('use', self.Action1)
        self.menu.addAction('sendback', self.Action2)
        self.menu.addAction('delete',self.deleteLater)
        self.setMenu(self.menu)


    def Action2(self):
        print("sendback position", self.geometry())

    def Action1(self):
        print("~")

    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)


class View(QGraphicsView):
    def __init__(self, parent):
        QGraphicsView.__init__(self, parent)
        self.setScene(QGraphicsScene(self))
        self.setAcceptDrops(True)
        self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
        self.btn1=DragButton('Test1', self)
        self.btn2=DragButton('Test2', self)
        self.btn2.setGeometry(230, 80, 100, 30)

如果您的按钮必须与视图共享信息,则可以创建新信号。