PyQt Drop Event 没有子类化? [英] PyQt Drop Event without subclassing?

查看:29
本文介绍了PyQt Drop Event 没有子类化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 python pyqt 代码按照我的意图工作.但是,我不喜欢对 QLineEdit 进行子类化,这样我就可以检测到 QLineEdit 字段上的文件丢失.我喜欢更优雅、更简单的连接"技术(新样式信号/槽处理),我用来检测编辑字段的文本更改.

This python pyqt code works how I intended. But, I don't like having to subclass QLineEdit so that I can detect file drop onto my QLineEdit field. I like the more elegant and simpler "connect" technique (new style signal/slot handling) that I used to detect text changes to the edit field.

我的问题:是否有信号/插槽连接解决方​​案来处理编辑字段上的丢弃,而无需子类化 QLineEdit?

My question: Is there a signal/slot connect solution for handling drops on the edit field without having to subclass QLineEdit?

另外,我必须在子类中实现这两个方法,这很烦人... dragEnterEvent &dropEvent 使放置工作!

Also, it is annoying that I must implement both methods in the subclass... dragEnterEvent & dropEvent to make the drop work!

import sys
from PyQt4 import QtGui, QtCore

class dropedit(QtGui.QLineEdit):   # subclass 
    def __init__(self, parent=None):
        super(dropedit, self).__init__(parent)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        print "dragEnterEvent:"

        if event.mimeData().hasUrls():
            event.accept()   # must accept the dragEnterEvent or else the dropEvent can't occur !!!
        else:
            event.ignore()

    def dropEvent(self, event):

        if event.mimeData().hasUrls():   # if file or link is dropped

            urlcount = len(event.mimeData().urls())  # count number of drops

            url = event.mimeData().urls()[0]   # get first url

            self.setText(url.toString())   # assign first url to editline

            #event.accept()  # doesnt appear to be needed

class testDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(testDialog, self).__init__(parent)


        form = QtGui.QFormLayout()
        form.setHorizontalSpacing(0)

        myedit = dropedit()
        form.addWidget(myedit)

        self.setLayout(form)
        self.setGeometry(300, 300, 400, 0)
        self.setWindowTitle('drop test')

        myedit.textChanged.connect(self.editchange)   # new style signal slot connections

    @QtCore.pyqtSlot(str)   # int represent the column value
    def editchange(self,data):
        print "editchange:", data

if __name__ == "__main__":

    app = QtGui.QApplication([])
    dl = testDialog()
    dl.exec_()
    sys.exit(app.closeAllWindows())

推荐答案

无需子类化:可以使用事件过滤器:

No need to subclass: you can use an event filter:

import sys
from PyQt4 import QtGui, QtCore

class testDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(testDialog, self).__init__(parent)

        form = QtGui.QFormLayout()
        form.setHorizontalSpacing(0)

        self.myedit = QtGui.QLineEdit()
        self.myedit.setDragEnabled(True)
        self.myedit.setAcceptDrops(True)
        self.myedit.installEventFilter(self)

        form.addWidget(self.myedit)

        self.setLayout(form)
        self.setGeometry(300, 300, 400, 0)
        self.setWindowTitle('drop test')

        self.myedit.textChanged.connect(self.editchange)   # new style signal slot connections

    @QtCore.pyqtSlot(str)   # int represent the column value
    def editchange(self,data):
        print "editchange:", data.toLatin1()

    def eventFilter(self, object, event):
        if (object is self.myedit):
            if (event.type() == QtCore.QEvent.DragEnter):
                if event.mimeData().hasUrls():
                    event.accept()   # must accept the dragEnterEvent or else the dropEvent can't occur !!!
                    print "accept"
                else:
                    event.ignore()
                    print "ignore"
            if (event.type() == QtCore.QEvent.Drop):
                if event.mimeData().hasUrls():   # if file or link is dropped
                    urlcount = len(event.mimeData().urls())  # count number of drops
                    url = event.mimeData().urls()[0]   # get first url
                    object.setText(url.toString())   # assign first url to editline
                    #event.accept()  # doesnt appear to be needed
            return False # lets the event continue to the edit
        return False

if __name__ == "__main__":

    app = QtGui.QApplication([])
    dl = testDialog()
    dl.exec_()
    sys.exit(app.closeAllWindows())

这篇关于PyQt Drop Event 没有子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆