用户界面不接受文件删除

时间:2018-08-17 21:41:37

标签: python pyside2

我想做什么:

我正在尝试创建一个PySide2 / PyQt窗口,该窗口接受放在其UI上的文件并运行一个函数。

问题:

UI不接受文件删除,也不触发定义的任何事件功能。 enter image description here

我到目前为止所做的事情:

下面是我的应用程序的精简代码,我在其中加载在Qt-Designer中创建的.ui文件以加载界面。

  • 窗口存储在self.window变量中。

  • self.window具有setAcceptDrops(True)。

  • 事件函数也已定义。

-代码

from PySide2 import QtUiTools
from PySide2 import QtGui
from PySide2 import QtCore
from PySide2 import QtWidgets

import os,sys
kUiFilePath = os.path.join(os.path.dirname(__file__) , 'uiFile.ui')


class LoadTool(QtWidgets.QMainWindow):
    def __init__(self, uiFilePath):
        self.window = None
        self.app = None
        self.loadUI(uiFilePath)
        self.window.setAcceptDrops(True)
        self.window.show()
        sys.exit(self.app.exec_())

    def dropEvent(self, e):
        print e
        e.accept()

    def dragEnterEvent(self, e):
        print e
        e.accept()

    def dragMoveEvent(self, e):
        print e
        e.accept()


    def loadUI(self, uiFilePath):        
        self.app = QtWidgets.QApplication(sys.argv)
        super(LoadTool, self).__init__()
        self.window = QtUiTools.QUiLoader().load(uiFilePath)

LoadTool(kUiFilePath)

UI文件代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ShellExport</class>
 <widget class="QDialog" name="ShellExport">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>399</width>
    <height>520</height>
   </rect>
  </property>
  <property name="mouseTracking">
   <bool>false</bool>
  </property>
  <property name="acceptDrops">
   <bool>true</bool>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>381</width>
     <height>501</height>
    </rect>
   </property>
   <property name="acceptDrops">
    <bool>true</bool>
   </property>
   <property name="title">
    <string>Device Status</string>
   </property>
   <widget class="QGroupBox" name="groupBox_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>30</y>
      <width>361</width>
      <height>111</height>
     </rect>
    </property>
    <property name="acceptDrops">
     <bool>true</bool>
    </property>
    <property name="title">
     <string>Deploy Fbx</string>
    </property>
    <widget class="QLineEdit" name="lineEdit">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>50</y>
       <width>341</width>
       <height>20</height>
      </rect>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
     <property name="geometry">
      <rect>
       <x>270</x>
       <y>80</y>
       <width>75</width>
       <height>23</height>
      </rect>
     </property>
     <property name="text">
      <string>Deploy fbx</string>
     </property>
    </widget>
    <widget class="QLabel" name="label_2">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>30</y>
       <width>47</width>
       <height>13</height>
      </rect>
     </property>
     <property name="text">
      <string>Fbx path</string>
     </property>
    </widget>
   </widget>
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>150</y>
      <width>201</width>
      <height>91</height>
     </rect>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:0)

这对您有用吗?

from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import QFile

from contextlib import closing
import sys


class DropWidget(QWidget):
    def __init__(self, parent=None):
        super(DropWidget, self).__init__(parent)

    def dropEvent(self, e):
        print("Drop", e)
        e.accept()

    def dragEnterEvent(self, e):
        print("dragEnter", e)
        e.accept()

    def dragMoveEvent(self, e):
        print("dragMove", e)
        e.accept()


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.ui_loader = QUiLoader()
        self.ui_loader.registerCustomWidget(DropWidget)
        self.ui_file = QFile('uiFile2.ui')
        with closing(self.ui_file) as self.qt_file:
            if self.qt_file.open(QFile.ReadOnly):
                self.main_win = self.ui_loader.load(self.qt_file)
            else:
                print('Failed to read UI')

    def show(self):
        self.main_win.show()


app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

<?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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="DropWidget" name="centralwidget">
   <property name="acceptDrops">
    <bool>true</bool>
   </property>
   <widget class="QGroupBox" name="groupBox">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>381</width>
      <height>501</height>
     </rect>
    </property>
    <property name="acceptDrops">
     <bool>true</bool>
    </property>
    <property name="title">
     <string>Device Status</string>
    </property>
    <widget class="QGroupBox" name="groupBox_2">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>30</y>
       <width>361</width>
       <height>111</height>
      </rect>
     </property>
     <property name="acceptDrops">
      <bool>true</bool>
     </property>
     <property name="title">
      <string>Deploy Fbx</string>
     </property>
     <widget class="QLineEdit" name="lineEdit">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>50</y>
        <width>341</width>
        <height>20</height>
       </rect>
      </property>
     </widget>
     <widget class="QPushButton" name="pushButton">
      <property name="geometry">
       <rect>
        <x>270</x>
        <y>80</y>
        <width>75</width>
        <height>23</height>
       </rect>
      </property>
      <property name="text">
       <string>Deploy fbx</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>30</y>
        <width>47</width>
        <height>13</height>
       </rect>
      </property>
      <property name="text">
       <string>Fbx path</string>
      </property>
     </widget>
    </widget>
    <widget class="QGraphicsView" name="graphicsView">
     <property name="geometry">
      <rect>
       <x>20</x>
       <y>150</y>
       <width>201</width>
       <height>91</height>
      </rect>
     </property>
    </widget>
   </widget>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>DropWidget</class>
   <extends>QWidget</extends>
   <header>dropwidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

使用UI文件存在一些陷阱:  1.主UI对象是加载程序返回的对象。所发布的代码不是为加载的对象而是为加载工具定义事件方法。  2.自定义窗口小部件必须先在ui_loader.registerCustomWidget中注册,然后才能使用 3.出现错误后停止加载UI文件

我不确定是否可以适应QDialog 请不要,这是针对Python 3.6的代码