0

+

我试图编写一个程序,在该程序中,按钮应通过拖动和按下来创建其他按钮,以形成滚动磨坊街的表示。我创建了一个 Button 类,它执行移动并处理创建中按钮的几个单击事件。这部分工作正常。我卡住的地方,我想读取应创建这些按钮的鼠标位置。
这是代码:

def createConnects(self):
    self.id000001.released.connect(self.do_something)
    self.id000002.released.connect(self.do_something)

@QtCore.pyqtSlot()
def do_something(self):
    self.bname = 'Button'
    self.button = Button(self.bname, self)

    posx = QtGui.QMouseEvent.x()
    print(posx)
    posy = QtGui.QMouseEvent.y
    print(posy)

    sender = self.sender()
    print(str(sender.objectName()))

    #self.button.move(posx, posy)

提供了几个函数,但是 posx 行给了我这个错误:
TypeError: QMouseEvent.x(): first argument of unbound method must have type 'QMouseEvent'

posy-line 给出了 <built-in function y > 这也不是我想要的,但很清楚。

MouseTracking 在MainWindow类中打开。

也许,通常人们会通过在 def 行中使用event来做到这一点,但由于它是一个插槽,这会导致其他问题。

有什么相处之道吗?

更新:建议这里是原型的完整代码:#!/usr/bin/env python3.4

import sys, os, math, shutil, re
from subprocess import call, Popen
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUiType
Ui_MainWindow, QMainWindow = loadUiType('mainwindow.ui')

def main(argv):
    app = QtWidgets.QApplication(argv)
    mainwindow = MyMainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

class Button(QtWidgets.QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.button = QtWidgets.QPushButton()

    def mouseMoveEvent(self, event):
        if event.buttons() != QtCore.Qt.RightButton:
            return
        mimeData = QtCore.QMimeData()
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(event.pos() - self.rect().topLeft())
        dropAction = drag.exec_(QtCore.Qt.MoveAction)

    def mousePressEvent(self, event):
        QtWidgets.QPushButton.mousePressEvent(self, event)
        if event.button() == QtCore.Qt.LeftButton:
            print(event.button(),' pressed')

class MyMainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self, *args):
        QtWidgets.QMainWindow.__init__(self, *args)
        self.setupUi(self)
        self.setMouseTracking(True)
        self.createConnects()

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

    def dropEvent(self, event):
        position = event.pos()
        self.button.move(position)
        event.setDropAction(QtCore.Qt.MoveAction)
        event.accept()

    def createConnects(self):
        self.id000001.released.connect(self.do_something)
        self.id000002.released.connect(self.do_something)

    @QtCore.pyqtSlot()
    def do_something(self):
        print('do_something')
        self.bname = 'Button'
        self.button = QtWidgets.QPushButton()
        #self.button = Button(self.bname, self)
        self.button.move(100, 65)
        #posx = QtGui.QMouseEvent.x()
        #print(posx)
        #posy = QtGui.QMouseEvent.y
        #print(posy)

        sender = self.sender()
        print(str(sender.objectName()))

        #self.button.move(posx, posy)



if __name__ == '__main__':
    main(sys.argv)

干杯,基督徒

4

0 回答 0