Python& PyQt的。剪贴板内容到listwidget

时间:2015-12-06 14:58:52

标签: python pyqt4 clipboard

我正在用PyQt4编写我的第一个python应用程序。我是python的初学者,通常是编程新手。基本上这个应用程序做的是监视剪贴板数据更改并将其作为项目放在QlistWidget中。您也可以通过复制粘贴数据并单击添加按钮手动添加项目。至于监视数据并自动将其添加到列表小部件中,我添加了一个条件,其中需要切换单选按钮以使“监视功能”执行其操作。添加项目的手动方式有效但监视功能似乎不起作用。它不会将数据(保存在剪贴板中)添加到列表小部件中。我想我似乎错过了一些东西。 我正在使用Python 2.7和PyQt 4.11

这是我的代码。我删除了额外的代码行(样式表& etc~)。

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

rbs = False
class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(385, 277)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.radioButton = QtGui.QRadioButton(Form)
        self.radioButton.setObjectName(_fromUtf8("radioButton"))
        self.gridLayout.addWidget(self.radioButton, 0, 1, 1, 1)

        self.radioButton.toggled.connect(self.toggledRB)

        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)

        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)

        self.pushButton.clicked.connect(self.addItem)

        self.listWidget = QtGui.QListWidget(Form)
        self.listWidget.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed|QtGui.QAbstractItemView.SelectedClicked)
        self.listWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
        self.listWidget.setMovement(QtGui.QListView.Snap)
        self.listWidget.setUniformItemSizes(False)
        self.listWidget.setObjectName(_fromUtf8("listView"))
        self.gridLayout.addWidget(self.listWidget, 2, 0, 1, 2)


        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))

        self.radioButton.setText(_translate("Form", "monitor clipboard", None))
        self.pushButton.setText(_translate("Form", "Add", None))

    def toggledRB(self, bool):
        global rbs
        if self.radioButton.isChecked():
            rbs = True
        else:
            rbs = False
        print rbs

    def addItem(self):
        yyy = self.lineEdit.text()
        self.listWidget.addItem(yyy)

    def dataReceiver(self, intel):
        print 'data is here!'
        self.listWidget.addItem(intel)

class clipboardListener(QObject):  

    @pyqtSlot()  
    def changedSlot(self):
        global rbs
        print 'clipboard data detected!'
        if rbs == True:
            intel = QApplication.clipboard().text()
            print intel
            xxx = Ui_Form()
            xxx.dataReceiver(intel)
        else:
            print 'False Alarm. There was no data. I was just kidding.'

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

    listener = clipboardListener()
    QObject.connect(QApplication.clipboard(),SIGNAL("dataChanged()"),listener,SLOT("changedSlot()"))

    sys.exit(app.exec_()) 

1 个答案:

答案 0 :(得分:1)

clibpboardListener中,您创建了Ui_Form的新实例,并在其上调用dataReceiver方法,而不是之前创建的ui实例。

要使其工作,您需要将要调用dataReceiver方法的对象传递给clipboardListener

另一个建议是:您应该使用new style signals而不是调用QObject.connect(...)。在python中,您不应该使用True==进行比较,要么直接使用布尔值作为条件,要么使用is True如果要明确检查True不是对比对象的隐含真值。

这有效:

...

class clipboardListener(QObject):  

    def __init__(self, target):
        QObject.__init__(self)
        self.target = target

    @pyqtSlot()  
    def changedSlot(self):
        global rbs
        print 'clipboard data detected!'
        if rbs:
            intel = QApplication.clipboard().text()
            print intel
            self.target.dataReceiver(intel)
        else:
            print 'False Alarm. There was no data. I was just kidding.'

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

    listener = clipboardListener(ui)

    QApplication.clipboard().dataChanged.connect(listener.changedSlot)

    sys.exit(app.exec_())