简体   繁体   English

在PySide中覆盖/重新实现插槽

[英]Overriding/Reimplementing Slots in PySide

I have almost the exact same question as the one found here: Override shouldInterruptJavaScript in QWebPage with PySide 我几乎有一个与在这里发现的问题完全相同的问题: 使用PySide覆盖QWebPage中的shouldInterruptJavaScript

In my case though I want to override the copy and paste slots on QLineEdit 就我而言,尽管我想覆盖QLineEdit上的复制和粘贴插槽

import sys
from PySide import QtGui, QtCore

class myLineEdit(QtGui.QLineEdit):
    # FIXME: This is not working, the slot is not overriden!
    @QtCore.Slot()
    def copy(self):
        print 'overridden copy event'
        App.clipboard().setText('customized text')
        return False

    @QtCore.Slot()
    def paste(self):
        print 'overridden paste event'
        self.setText('customized text')
        return False

if __name__ == "__main__":
    App = QtGui.QApplication(sys.argv)
    Widget = myLineEdit()
    Widget.show()
    cmenu = Widget.createStandardContextMenu()
    sys.exit(App.exec_())

I'm using Python 2.7.3, with PySide 1.2.2 我正在使用Python 2.7.3和PySide 1.2.2

I don't know if these methods are even supposed to be override-able, but I can't find any documentation that says they shouldn't be. 我不知道这些方法是否应该可以重写,但是我找不到任何文档说明它们不应该被重写。

I also found this page http://qt-project.org/faq/answer/is_it_possible_to_reimplement_non-virtual_slots 我还发现了此页面http://qt-project.org/faq/answer/is_it_possible_to_reimplement_non-virtual_slots

The page explains how certain kinds of slots get pointers set to them by functions that get called when the object is initialized (or something along those lines, I'm not as familiar with the C++). 该页面解释了某些类型的插槽如何通过初始化对象时调用的函数来设置指向它们的指针(或者类似的话,我对C ++并不熟悉)。

Following this logic I added the createStandardContextMenu() call above in the hopes that it would reinitialize the slots for at least the context menu, but no luck. 按照这种逻辑,我在上面添加了createStandardContextMenu()调用,希望它至少可以为上下文菜单重新初始化插槽,但是没有运气。

Am I doing something wrong? 难道我做错了什么? Or should I try filing a bug report? 还是应该尝试提交错误报告?

You cannot override QLineEdit.copy or QLineEdit.paste in such a way that they will be called internally by Qt. 您不能以Qt在内部调用QLineEdit.copyQLineEdit.paste的方式覆盖它们。

In general, you can only usefully override or reimplement Qt functions that are defined as being virtual . 通常,您只能有用地覆盖或重新实现定义为virtual的 Qt函数。 The Qt Docs will always specify whether this is the case, and for QLineEdit , there are no public slots that are defined in that way. Qt Docs将始终指定是否是这种情况,并且对于QLineEdit ,没有以这种方式定义的公共插槽

There is also no easy workaround. 也没有简单的解决方法。 There are a lot of different ways in which copy and paste operations (or their equivalents) can be invoked, such as keyboard shortcuts, context menu, drag and drop, etc. It can be done: but it's a lot of work to get complete control over all these sorts of operations. 有很多不同的方式复制和粘贴操作(或它们的等价物)可以被调用,如键盘快捷键,上下文菜单,拖放等,可以做到:但它是一个大量的工作,以便获得完整控制所有这些类型的操作。 So you need to think carefully about what you're trying to achieve before deciding how to proceed. 因此,在决定如何进行之前,您需要仔细考虑您要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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