0
votes

I am currently making a program where a user selects an image qpushbutton. I already have superseded mouseMoveEvent, mousePressEvent, and mouseReleaseEvent in the button class to get a movable button. The buttons are currently moving independently, but I would like the buttons to move so that the horizontal distance between them stays the same.

So currently in pseudo code I have:

import stuff
import mvbutton as mv

class MasterClass(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        #more setup stuff, layout, etc
        self.addbutton(image,name,size)
    def addbutton(#args):
        self.button=mv.dragbutton(#args)
        #some more setup
        #now rename so that each button has its own name
        if name== "name1":
            self.name1=self.button
        else:
            self.name2=self.button
        self.button=""
    #more code to set up

I supersede the mouse motion/press/release functions in the dragbutton class. I cannot, therefore reference the new self.name# there. So the self.move(pos) in my dragbutton class cannot get the self.name# because it is a different self. Any ideas on how I could get this to work? Thanks.

1
have you tried adding the two buttons to a widget and instead of moving the individual buttons.. just move the widget. also, in the widget while adding the buttons to the layout, just define the spacing. that way, the horizontal spacing will remain constant.smitkpatel
Yeah, I am going to have them space the buttons a set amount. I'll try the Qwidget method.IronManMark20
How would I implement this? I haven't ever needed to do so. Am I supposed to just make a QWidget class and have the buttons attach to that widget? I am not sure how I would do this.IronManMark20
@smitkpatel, would I setup a QWidget and then embed the buttons in that QWidget (if so, how?).IronManMark20

1 Answers

0
votes

Done something very rough after trying to understand your requirement. Hope this helps.

EDIT

tried to add more accuracy in moving. Won't do real time moving cause it has problems with lag and update. I guess the moving won't be jittery any more.

from PyQt4 import QtGui
import sys
class MultiButton(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self._b1 = QtGui.QPushButton("B1")
        self._b2 = QtGui.QPushButton("B2")
        self._arrangeWidgets()
        self.setStyleSheet("background-color: rgb(0, 0, 0);\n"+\
                           "color: rgb(255, 255, 255);\n"+\
                           "border:1px solid #7F462C ;\n")
        self._moveStart = False
        self._startX = 0
        self._startY = 0

    def _arrangeWidgets(self):
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self._b1)
        #horizontal spacing remains constant now
        layout.addSpacing(90)
        layout.addWidget(self._b2)
        self.setLayout(layout)

    def mousePressEvent(self,event):
        self._moveStart = True 
        self._startX = event.pos().x() - self.pos().x()
        self._startY = event.pos().y() - self.pos().y()
        return QtGui.QWidget.mousePressEvent(self, event)
    

    def mouseReleaseEvent(self, event):
        if self._moveStart:
            self.setGeometry(event.pos().x() - self._startX,event.pos().y() - self._startY,self.width(),self.height())
            self._moveStart = False
            self._startX = 0
            self._startY = 0
            
        return QtGui.QWidget.mouseReleaseEvent(self, event)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    wd = QtGui.QMainWindow()
    wd.resize(500,500)
    mb = MultiButton()
    mb.setFixedSize(200,50)
    wd.setCentralWidget(mb)
    wd.show()
    sys.exit(app.exec_())

here the MultiButton widget moves the two buttons keeping the horizontal space between the two always constant.