简体   繁体   中英

PyQT QListWidget drag and drop does not work

I am trying to implement in my project a QListWidget with the possibility of moving elements by drag and drop

I try to integrate it into the project in the simplest way without success, while outside I have no problem executing it.

EDIT:The problem seems to come from the realsense library, without its, DAD works

Here is its implementation:

priorityContainer.py:

class priorityContainer(QListWidget):
def __init__(self):
    super().__init__()
    self.setIconSize(QSize(124, 124))
    self.setDragDropMode(QAbstractItemView.InternalMove)
    self.setDefaultDropAction(Qt.MoveAction) 
    self.setSelectionMode(QAbstractItemView.ExtendedSelection)
    self.setAcceptDrops(True)
    self.setDragEnabled(True)

    for i in range(5):
        QListWidgetItem( 'Item '+str(i), self)

main_interface.py:

# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
import traceback, sys, os
import pyrealsense2 as rs
from ressource.interface import priorityContainer


class UI_main(QMainWindow):
    def __init__(self):
        super(UI_main, self).__init__()

        self.setupUi()
        self.show()

    def setupUi(self):
        self.centralwidget = QWidget(self)
        self.mainVcontainer = QVBoxLayout(self.centralwidget)

        self.listWidget = priorityContainer.priorityContainer()

        self.mainVcontainer.addWidget(self.listWidget)
        self.setCentralWidget(self.centralwidget)

    def root_path(self):
        return os.path.abspath(os.sep)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = UI_main()
    sys.exit(app.exec_())

I have solved my problem by adding these lines before any other imports where I import my pyrealsense2 librairies:

import sys
sys.coinit_flags = 2
import pythoncom

Reference to the fix: https://github.com/IntelRealSense/librealsense/issues/6174

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM