简体   繁体   中英

Overriding/Reimplementing Slots in PySide

I have almost the exact same question as the one found here: Override shouldInterruptJavaScript in QWebPage with PySide

In my case though I want to override the copy and paste slots on 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

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

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++).

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.

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.

In general, you can only usefully override or reimplement Qt functions that are defined as being virtual . 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.

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.

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