1
我需要能夠在背景圖像頂部的屏幕上顯示控件。我已經完成了該任務,並且已經創建並顯示了一個控件(一個顯示圖像的QLabel)。現在我需要能夠通過將標籤拖動到我想要移動它的位置來移動標籤,我已經遵循了幾個關於在pyqt中進行拖放的教程,但是我未能完成此任務在QFrame中移動(拖動)QLabel
這是我的代碼。請注意,控件可以移動,但是當您移動它時,背景也會移動,當您放下它時,它會保持原位。我要的是隻移動(在QLabel顯示圖像)的控制和移動它只是拖動標籤內:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class CentralWidget(QFrame):
def __init__(self, *args):
super(CentralWidget, self).__init__(*args)
self.setStyleSheet("background-image: url(logo.png);")
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
# get the relative position from the mime data
mime = e.mimeData().text()
x, y = map(int, mime.split(','))
# move
# so move the dragged button (i.e. event.source())
e.source().move(e.pos()-QPoint(x, y))
# set the drop action as Move
e.setDropAction(Qt.MoveAction)
# tell the QDrag we accepted it
e.accept()
class Selector(QLabel):
def mouseMoveEvent(self, e):
if e.buttons() != Qt.LeftButton:
return
# write the relative cursor position to mime data
mimeData = QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))
# let's make it fancy. we'll show a "ghost" of the button as we drag
# grab the button to a pixmap
pixmap = QPixmap.grabWidget(self)
# below makes the pixmap half transparent
painter = QPainter(pixmap)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(pixmap.rect(), QColor(0, 0, 0, 127))
painter.end()
# make a QDrag
drag = QDrag(self)
# put our MimeData
drag.setMimeData(mimeData)
# set its Pixmap
drag.setPixmap(pixmap)
# shift the Pixmap so that it coincides with the cursor position
drag.setHotSpot(e.pos())
# start the drag operation
# exec_ will return the accepted action from dropEvent
if drag.exec_(Qt.MoveAction) == Qt.MoveAction:
print 'moved'
# else:
# print 'copied'
def mousePressEvent(self, e):
QLabel.mousePressEvent(self,e)
if e.button() == Qt.LeftButton:
print 'press'
class fPrincipal(QMainWindow):
def __init__(self, parent=None):
# Call base class constructor
QMainWindow.__init__(self, parent)
self.setGeometry(QRect(0, 0, 599+10, 399+10))
self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())
# Creamos el contenedor central, que sera organizado por pestañas
centralWidget = QTabWidget()
self.setCentralWidget(centralWidget);
# Creamos la 1ra pestaña
tab = CentralWidget()
tabLayout = QHBoxLayout()
tab.setLayout(tabLayout)
# Añadimos la pestaña al contenedor central
centralWidget.addTab(tab,"Escena 1")
logDockWidget = QDockWidget("Tools", self)
logDockWidget.setObjectName("LogDockWidget")
logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget)
def crearMenu():
mimenu = self.menuBar().addMenu("&Archivo")
crearMenu()
selectorLb = Selector()
picture = QPixmap('D:\Adrian\Tesis\Codigo\selector.png')
selectorLb.setPixmap(picture)
tabLayout.addWidget(selectorLb)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = fPrincipal()
w.show()
sys.exit(app.exec_())
謝謝@ X.Jacobs你的回答非常好:D – 2013-03-25 20:40:42
@Adrián如果它很好,也許接受它並且贊成它。 – neuronet 2014-07-05 14:25:43