[PyQt] PyQt + Maya - copy/paste issue

Taylor Carrasco crackerbunny at gmail.com
Wed Jan 5 03:03:09 GMT 2011


I'm trying to copy text from Maya's script editor into a PyQt LineEdit (or
any text widget for that matter).

I've run into an interesting problem. If you run this widget outside Maya,
you can copy/paste from Maya, into the line edit just fine.
*Clipboard: text from script editor type: <class 'PyQt4.QtCore.QString'>*


However, if you run it from within Maya, it hangs for a short time, and
expresses nothing is available in the QClipboard....
*Clipboard:    type: <class 'PyQt4.QtCore.QString'>*



import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def inMaya():
    try:
        import maya.cmds as mc
        mc.about(application=True)
        return True
    except:
        return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        self.le = MyLineEdit()
        layout = QVBoxLayout()
        layout.addWidget(self.le)
        self.setLayout(layout)

class MyLineEdit(QLineEdit):
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)

        self.obj = QLineEdit()
        self.connect(self, SIGNAL('cntrlVPressed'), self.onCntrlVPressed)

    def event(self, e):
        # Check if keyPressed, control is held down, and key "v" is pressed
        if e.type()==QEvent.KeyPress and e.modifiers() == Qt.ControlModifier
and e.key()==Qt.Key_V:
            self.emit(SIGNAL('
cntrlVPressed'))
            return True

        return QLineEdit.event(self, e)

    def onCntrlVPressed(self):

        myClipBoard = QApplication.clipboard()
        pastedText = myClipBoard.text('plain', QClipboard.Selection)

        print 'Clipboard: ', pastedText, ' type:' , type(pastedText)
        print

if inMaya():
    w = MyWindow()
    w.show()
else:
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110105/ea31cdcc/attachment.html>


More information about the PyQt mailing list