Guest User

Untitled

a guest
Apr 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. # Override
  2. def dragEnterEvent(self, event):
  3. if event.mimeData().hasFormat("text/plain"):
  4. event.acceptProposedAction()
  5. print("Drag entered at: " + str(event.pos()))
  6.  
  7. import sys
  8. import os
  9. from PyQt5.QtCore import *
  10. from PyQt5.QtGui import *
  11. from PyQt5.QtWidgets import *
  12.  
  13.  
  14. '''================================================================================'''
  15. '''| CUSTOM QLINE-EDIT |'''
  16. '''================================================================================'''
  17. class CustomLineEdit(QLineEdit):
  18. def __init__(self, *args, **kwargs):
  19. super(CustomLineEdit, self).__init__(*args, **kwargs)
  20. self.setReadOnly(True)
  21. self.setContextMenuPolicy(Qt.NoContextMenu)
  22. self.setMouseTracking(True)
  23. self.setCursorPosition(0)
  24. self.dragStartPosition = 0
  25. self.setAcceptDrops(True)
  26. self.setFixedHeight(50)
  27. self.setFixedWidth(300)
  28.  
  29. ''''''
  30.  
  31. def mousePressEvent(self, event):
  32. super(CustomLineEdit, self).mousePressEvent(event)
  33. if (event.button() == Qt.RightButton) or (event.modifiers() & Qt.ControlModifier):
  34. return
  35. else:
  36. self.dragStartPosition = event.pos()
  37. ###
  38.  
  39. ''''''
  40.  
  41. def mouseMoveEvent(self, event):
  42. event.accept()
  43. if event.buttons() == Qt.NoButton:
  44. return
  45. if (event.pos() - self.dragStartPosition).manhattanLength() < QApplication.startDragDistance():
  46. return
  47.  
  48. # Start dragging
  49. # ---------------
  50. drag = QDrag(self)
  51. drag.setPixmap(QPixmap("cmd.png"))
  52. mimeData = QMimeData()
  53. mimeData.setText("my mime data")
  54. drag.setMimeData(mimeData)
  55. dropAction = drag.exec(Qt.CopyAction | Qt.MoveAction)
  56.  
  57. ''''''
  58.  
  59. def dragEnterEvent(self, event):
  60. if event.mimeData().hasFormat("text/plain"):
  61. event.acceptProposedAction()
  62. print("Drag entered at: " + str(event.pos()))
  63. ###
  64.  
  65. ''''''
  66.  
  67. def dragLeaveEvent(self, event):
  68. event.accept()
  69. # print("Drag left at: " + str(event.pos())) # <- doesn't work :-(
  70. print("Drag left at: ?")
  71.  
  72. ''''''
  73.  
  74. def dropEvent(self, event):
  75. if event.mimeData().hasFormat("text/plain"):
  76. event.acceptProposedAction()
  77. print("Drag dropped at: " + str(event.pos()))
  78. ###
  79.  
  80. ''''''
  81.  
  82.  
  83. '''================================================================================'''
  84. '''| CUSTOM MAIN WINDOW |'''
  85. '''================================================================================'''
  86. class CustomMainWindow(QMainWindow):
  87.  
  88. def __init__(self):
  89. super(CustomMainWindow, self).__init__()
  90.  
  91. # -------------------------------- #
  92. # Window setup #
  93. # -------------------------------- #
  94.  
  95. # 1. Define the geometry of the main window
  96. # ------------------------------------------
  97. self.setGeometry(100, 100, 800, 200)
  98. self.setWindowTitle("QLineEdit test")
  99.  
  100. # 2. Create frame and layout
  101. # ---------------------------
  102. self.__frm = QFrame(self)
  103. self.__frm.setStyleSheet("QWidget { background-color: #ffffff }")
  104. self.__lyt = QVBoxLayout()
  105. self.__lyt.setAlignment(Qt.AlignTop)
  106. self.__frm.setLayout(self.__lyt)
  107. self.setCentralWidget(self.__frm)
  108.  
  109. # 3. Create QLineEdit
  110. # -------------------
  111. self.__myQLineEdit = CustomLineEdit()
  112. self.__lyt.addWidget(self.__myQLineEdit)
  113.  
  114.  
  115. self.show()
  116.  
  117. '''=== end Class ==='''
  118.  
  119.  
  120. if __name__ == '__main__':
  121. app = QApplication(sys.argv)
  122. QApplication.setStyle(QStyleFactory.create('Fusion'))
  123. myGUI = CustomMainWindow()
  124. sys.exit(app.exec_())
  125.  
  126. ''''''
  127.  
  128. Drag entered at: PyQt5.QtCore.QPoint(100, 22)
  129. Drag left at: ?
  130. Drag entered at: PyQt5.QtCore.QPoint(132, 49)
  131. Drag left at: ?
  132. Drag entered at: PyQt5.QtCore.QPoint(229, 49)
  133. Drag left at: ?
  134. Drag entered at: PyQt5.QtCore.QPoint(242, 49)
  135. Drag dropped at: PyQt5.QtCore.QPoint(230, 23)
Add Comment
Please, Sign In to add comment