Qiao
Qiao

Reputation: 17049

PyQt save to clipboard in a slot

How can I save some text to clipboard by pressing button? clipboard.setText("gg") works by itself

widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard.setText("text") )

throw error, you can only use instance.methodName

widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard, QtCore.SLOT('setText("text")') )

do nothing.

What is wrong?

Upvotes: 0

Views: 530

Answers (1)

JBernardo
JBernardo

Reputation: 33397

First, there's a much better way to connect signals to slots on PyQt:

button.clicked.connect(self.method)

You can use lambda functions to pass extra arguments to methods. Then you call

button1.clicked.connect(lambda : clipboard.setText('btn one'))
button2.clicked.connect(lambda : clipboard.setText('btn two'))

When you pass a function call, in fact the interpreter is evaluating the call and trying to pass the result to the SIGNAL/SLOT connection. That's why your first example doesn't work.

I've written something similar here: https://stackoverflow.com/questions/...from-other-functions

Upvotes: 1

Related Questions