I'm making an application in python 3.5 and Qt5.7 with two QTableWidgets and I want to be able to drop items from one of the tables into the other one. I set up the drag and drop settings on both tables and everything works correctly until I press a modifier key (Ctrl, Alt, Shift, etc). From this moment on the drag and drop feature stops working.
I've already tried to subclass the table and reimplement the dnd events (QDragEnterEvent, QDragMoveEvent, QDropEvent) without success. I also tried to reimplement the mouse events using mousePreseEvent to catch the drag starting point and mouseMoveEvent to create a drag with the correct mimetype, and to create an eventFilter to catch the modifier keys and disable them when needed but none of them worked.
Using the drag and drop events I can see that after pressing any of the modifier keys when you start a drag movement, the drag event is created but just after that the drop event is created too, and the dnd stops without not even moving the mouse.
Does anyone have a solution or knows the reason for this behaviour?
I attach a simple code generated with pyuic5 from Qt5.7 designer with two QListWidgets where the same problem can be observed.
from PyQt5 import QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setDragEnabled(True)
self.listWidget.setDragDropMode(QtWidgets.QAbstractItemView.DropOnly)
item = QtWidgets.QListWidgetItem()
item.setText("1")
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
item.setText("2")
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
item.setText("3")
self.listWidget.addItem(item)
self.listWidget.setSortingEnabled(False)
self.horizontalLayout.addWidget(self.listWidget)
self.listWidget_2 = QtWidgets.QListWidget(self.centralwidget)
self.listWidget_2.setDragEnabled(True)
self.listWidget_2.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.listWidget_2.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
item = QtWidgets.QListWidgetItem()
item.setText("1")
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
item.setText("2")
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
item.setText("3")
self.listWidget_2.addItem(item)
self.listWidget_2.setSortingEnabled(False)
self.horizontalLayout.addWidget(self.listWidget_2)
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Just for reference the development system is a machine with Windows 10.
Finally I found the cause of the problem and was nothing related with any of the frameworks. It was caused by a software for using more than one computer with the same mouse and keyboard over the network.