【发布时间】:2018-06-17 13:01:24
【问题描述】:
我有一个主要的 pyqt5 应用程序。
单击按钮时我想调用 QDialog 并在提交 QDialog 时想在主应用程序中捕获 QDialog 的结果。
下面是一个 pyqt 模块,我喜欢将它添加到 QDialog,这可能吗,并且想在主应用程序中也捕获结果?
我不知道从哪里开始。
import sys
from PyQt5.QtCore import QSize
from PyQt5 import QtWidgets, QtCore,QtGui
from PyQt5.QtGui import QColor, QDrag, QPainter, QPixmap
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtWidgets
from win32api import GetSystemMetrics
from PyQt5.QtGui import QImage, QPalette, QBrush
w=GetSystemMetrics(0)/1366
h=GetSystemMetrics(1)/768
class Button(QtWidgets.QPushButton):
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.LeftButton:
return
# write the relative cursor position to mime data
mimeData = QtCore.QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))
pixmap = QtWidgets.QWidget.grab(self)
# below makes the pixmap half transparent
painter = QtGui.QPainter(pixmap)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
painter.end()
# make a QDrag
drag = QtGui.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_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
print('moved')
else:
print('copied')
class Example(QtWidgets.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
button = Button('Draggable Text', self)
button.setStyleSheet("font-size: 15px;background-color: rgb(255,255,255); margin:5px; border:1px solid rgb(255, 255, 255); ")
button.move(100, 65)
self.buttons = [button]
self.setWindowTitle('Copy or Move')
self.setFixedSize(960, 515)
oImage = QImage(r"C:\Users\vaas\Desktop\background.jpg")
sImage = oImage.scaled(QSize(960*w/1,515*h/1)) # resize Image to widgets size
palette = QPalette()
palette.setBrush(QPalette.ColorRole(10), QBrush(sImage)) # 10 = Windowrole
self.setPalette(palette)
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(','))
if e.keyboardModifiers() & QtCore.Qt.ShiftModifier:
# copy
# so create a new button
button = Button('Button', self)
# move it to the position adjusted with the cursor position at drag
button.move(e.pos()-QtCore.QPoint(x, y))
# show it
button.show()
# store it
self.buttons.append(button)
# set the drop action as Copy
e.setDropAction(QtCore.Qt.CopyAction)
else:
# move
# so move the dragged button (i.e. event.source())
print('mine°',e.pos())
e.source().move(e.pos()-QtCore.QPoint(x, y))
# set the drop action as Move
e.setDropAction(QtCore.Qt.MoveAction)
# tell the QDrag we accepted it
e.accept()
def main():
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
【问题讨论】:
-
您可以更好地解释我,我看到按钮被拖动了,您的代码中缺少什么?