python3.4 + pyqt5: Getting x,y out of QMouseEvent

1.2k views Asked by At

+

I'm trying to write a program in which buttons shall create other buttons by dragging and press_releasing to form a representation of a rolling mill street. I created a class Button which does the moving and handles several clicking events for the buttons in creation. This part is working fine. Where I'm stuck is, I want to read the mouse position where these button should be created.
Here's the code:

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)

provides several functions, but the posx line gives me this error:
TypeError: QMouseEvent.x(): first argument of unbound method must have type 'QMouseEvent'

posy-line gives < built-in function y > which isn't what I want, too, but clear.

MouseTracking is switched on in MainWindow class.

Maybe, normaly one would do that by using event in the def line but since it's a slot, that would lead to other problems.

Any hints to get along?

Update: As recommended here's the full code of the prototype: #!/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)

cheers, Christian

0

There are 0 answers