[英]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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.