python - 在 Python 中使用 QT Designer (.ui) 文件时如何启用 QDropEvent?

标签 python pyqt pyqt5 qt-designer qcombobox

使用 this 中的答案当不使用 QT Designer (.ui) 文件时,我已经能够触发 QDropEvent ;但我似乎无法让它正常工作。

根据this documentationthis example看来我只需要为 QComboBox 设置 .setAcceptDrops()True 我想接受该事件,并实现 dragEnterEventdropEvent 以及所有必要的逻辑。我对 C++ 不太熟悉,所以我只理解一些示例,但大部分内容看起来很简单。

我为此创建的示例不会引发任何异常,当我尝试将文本文件从 Windows 资源管理器拖放到 cb1 QComboBox 时,似乎没有发生任何情况。我觉得我错过了一些简单的东西,但我不确定是什么。有什么想法吗?

使用 Python 3.7 和 PyQt5

main.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>269</width>
    <height>116</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="3" column="1">
     <widget class="QComboBox" name="cb4">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="1" column="1">
     <widget class="QComboBox" name="cb2">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="2" column="1">
     <widget class="QComboBox" name="cb3">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QComboBox" name="cb1">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="0" column="0">
     <widget class="QLabel" name="l1">
      <property name="text">
       <string>cb1</string>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="l2">
      <property name="text">
       <string>cb2</string>
      </property>
     </widget>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="l3">
      <property name="text">
       <string>cb3</string>
      </property>
     </widget>
    </item>
    <item row="3" column="0">
     <widget class="QLabel" name="l4">
      <property name="text">
       <string>cb4</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

QComboBoxDrop.py

from PyQt5 import uic
from PyQt5.Qt import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

Ui_main = uic.loadUiType(r'main.ui')[0]


class MinExample(QMainWindow, Ui_main):
    def __init__(self, parent = None, flags = Qt.Window):
        QMainWindow.__init__(self, parent, flags)
        self.setupUi(self)

        self.cb1.addItems(["item {}".format(i) for i in range(5)])
        self.cb2.addItems(["item {}".format(i) for i in range(5)])
        self.cb3.addItems(["item {}".format(i) for i in range(5)])
        self.cb4.addItems(["item {}".format(i) for i in range(5)])

        self.cb1.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        print("dragEnterEvent Occurred")
        event.acceptProposedAction()

    def dropEvent(self, event):
        print("dropEvent Occurred")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MinExample()
    w.show()
    sys.exit(app.exec_())

最佳答案

如果您已经有一个类实现了您想要的逻辑并希望在 Qt Designer 中使用它,正确的选项是使用提升,因为它遵循此答案和步骤 4.1 中的步骤更改为以下内容:

enter image description here

另一方面,如果小部件是窗口的子窗口,我对旧帖子的解决方案将不起作用,在这种情况下,您必须覆盖 DragMoveEvent 方法。

考虑到上述情况,解决方案是:

├── main.py
├── main.ui
└── qcomboboxdrop.py

ma​​in.py

from PyQt5 import uic, QtCore, QtWidgets

Ui_main = uic.loadUiType(r'main.ui')[0]


class MinExample(QtWidgets.QMainWindow, Ui_main):
    def __init__(self, parent = None, flags = QtCore.Qt.Window):
        super(MinExample, self).__init__(parent, flags)
        self.setupUi(self)

        self.cb1.addItems(["item {}".format(i) for i in range(5)])
        self.cb2.addItems(["item {}".format(i) for i in range(5)])
        self.cb3.addItems(["item {}".format(i) for i in range(5)])
        self.cb4.addItems(["item {}".format(i) for i in range(5)])

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MinExample()
    w.show()
    sys.exit(app.exec_())

ma​​in.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>269</width>
    <height>148</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="3" column="1">
     <widget class="QComboBoxDrop" name="cb4">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="1" column="1">
     <widget class="QComboBoxDrop" name="cb2">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="2" column="1">
     <widget class="QComboBoxDrop" name="cb3">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QComboBoxDrop" name="cb1">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
    <item row="0" column="0">
     <widget class="QLabel" name="l1">
      <property name="text">
       <string>cb1</string>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="l2">
      <property name="text">
       <string>cb2</string>
      </property>
     </widget>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="l3">
      <property name="text">
       <string>cb3</string>
      </property>
     </widget>
    </item>
    <item row="3" column="0">
     <widget class="QLabel" name="l4">
      <property name="text">
       <string>cb4</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QComboBoxDrop</class>
   <extends>QComboBox</extends>
   <header>qcomboboxdrop.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

qcomboboxdrop.py

from PyQt5 import QtCore, QtWidgets

class QComboBoxDrop(QtWidgets.QComboBox):
    def __init__(self, *args, **kwargs):
        super(QComboBoxDrop, self).__init__(*args, **kwargs)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
         # print("formats: ", event.mimeData().formats())
        if event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()

    def dragMoveEvent(self, event):
        if event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()        

    def dropEvent(self, event):
        url =QtCore.QUrl(event.mimeData().text().strip())
        if url.isLocalFile():
            file = QtCore.QFile(url.toLocalFile())
            if file.open(QtCore.QFile.ReadOnly|QtCore.QFile.Text):
                ts = QtCore.QTextStream(file)
                while not ts.atEnd():
                    print(ts.readLine())

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = QComboBoxDrop()
    w.addItems(["item {}".format(i) for i in range(10)])
    w.show()
    sys.exit(app.exec_())

关于python - 在 Python 中使用 QT Designer (.ui) 文件时如何启用 QDropEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53973670/

相关文章:

python - 前一个 GUI 框架的 'Back' 按钮的 PyQt 适当函数

Python time.sleep(1) 立即完成

python - 为什么不能在 Python 中的 for 循环之前使用分号?

python - 根据构造函数签名使用不同的类定义

Python: object.__new__() 没有参数

python - QComboBox setMaxVisible() 不适用于融合风格

python - 如何从 QLineEdit() 获取 int?

Python:在PyQt4之谜中嵌入Chaco

python - pyqt:移动QToolBar图标?

python - 将 QHBoxLayout 的每个小部件对齐到顶部