如何改变按钮的位置
How to change the position of the buttons
在我的程序中,我有一组按钮,每个按钮都可以重新定位。我成功地改变了一个按钮的位置。如何更改所选按钮的位置。
比如拖动按钮1时,只有按钮1移动
拖动按钮 2 时,只有按钮 2 移动
我的代码:
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtQuick
from PySide2.QtWidgets import *
from PySide2.QtCore import *
class mainHMI (QWidget):
def __init__(self):
super().__init__()
self.button = {}
self.width = 705
self.height = 437
self.Label = QLabel(self)
self.Label.resize(self.width, self.height)
#Add Button
mainHMI.fun_button(self)
#show
self.show()
def fun_button (self) :
i=5
self.nbre_Lighthing_in_floor0 = 10
for j in range(self.nbre_Lighthing_in_floor0) :
self.button[j] = QPushButton(self.Label)
self.button[j].setGeometry(QRect(i, 90, 50, 50))
self.button[j].setMinimumSize(QSize(55, 55))
self.button[j].setMaximumSize(QSize(55, 55))
self.button[j].setText("button"+str(j+1))
self.button[j].setObjectName("button"+str(j))
self.button[j].show()
i = i + 70
def mouseMoveEvent(self,vent):
self.button[0].move(vent.x(),vent.y())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = mainHMI()
sys.exit(app.exec_())
在 QPushButton 子类中重新实现 mouseMoveEvent
以移动按钮。
class MovableButton(QPushButton):
def mousePressEvent(self, event):
self.origin = event.pos()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.move(event.windowPos().toPoint() - self.origin)
super().mouseMoveEvent(event)
class mainHMI(QWidget):
def __init__(self):
super().__init__()
self.button = {}
self.width = 705
self.height = 437
self.Label = QLabel(self)
self.Label.resize(self.width, self.height)
#Add Button
mainHMI.fun_button(self)
#show
self.show()
def fun_button (self) :
i=5
self.nbre_Lighthing_in_floor0 = 10
for j in range(self.nbre_Lighthing_in_floor0) :
self.button[j] = MovableButton(self.Label)
self.button[j].setGeometry(QRect(i, 90, 50, 50))
self.button[j].setMinimumSize(QSize(55, 55))
self.button[j].setMaximumSize(QSize(55, 55))
self.button[j].setText("button"+str(j+1))
self.button[j].setObjectName("button"+str(j))
self.button[j].show()
i = i + 70
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = mainHMI()
sys.exit(app.exec_())
在我的程序中,我有一组按钮,每个按钮都可以重新定位。我成功地改变了一个按钮的位置。如何更改所选按钮的位置。
比如拖动按钮1时,只有按钮1移动 拖动按钮 2 时,只有按钮 2 移动
我的代码:
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtQuick
from PySide2.QtWidgets import *
from PySide2.QtCore import *
class mainHMI (QWidget):
def __init__(self):
super().__init__()
self.button = {}
self.width = 705
self.height = 437
self.Label = QLabel(self)
self.Label.resize(self.width, self.height)
#Add Button
mainHMI.fun_button(self)
#show
self.show()
def fun_button (self) :
i=5
self.nbre_Lighthing_in_floor0 = 10
for j in range(self.nbre_Lighthing_in_floor0) :
self.button[j] = QPushButton(self.Label)
self.button[j].setGeometry(QRect(i, 90, 50, 50))
self.button[j].setMinimumSize(QSize(55, 55))
self.button[j].setMaximumSize(QSize(55, 55))
self.button[j].setText("button"+str(j+1))
self.button[j].setObjectName("button"+str(j))
self.button[j].show()
i = i + 70
def mouseMoveEvent(self,vent):
self.button[0].move(vent.x(),vent.y())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = mainHMI()
sys.exit(app.exec_())
在 QPushButton 子类中重新实现 mouseMoveEvent
以移动按钮。
class MovableButton(QPushButton):
def mousePressEvent(self, event):
self.origin = event.pos()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.move(event.windowPos().toPoint() - self.origin)
super().mouseMoveEvent(event)
class mainHMI(QWidget):
def __init__(self):
super().__init__()
self.button = {}
self.width = 705
self.height = 437
self.Label = QLabel(self)
self.Label.resize(self.width, self.height)
#Add Button
mainHMI.fun_button(self)
#show
self.show()
def fun_button (self) :
i=5
self.nbre_Lighthing_in_floor0 = 10
for j in range(self.nbre_Lighthing_in_floor0) :
self.button[j] = MovableButton(self.Label)
self.button[j].setGeometry(QRect(i, 90, 50, 50))
self.button[j].setMinimumSize(QSize(55, 55))
self.button[j].setMaximumSize(QSize(55, 55))
self.button[j].setText("button"+str(j+1))
self.button[j].setObjectName("button"+str(j))
self.button[j].show()
i = i + 70
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = mainHMI()
sys.exit(app.exec_())