- PyQt5 Tutorial
- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - QClipboard
The QClipboard class provides access to system-wide clipboard that offers a simple mechanism to copy and paste data between applications. Its action is similar to QDrag class and uses similar data types.
QApplication class has a static method clipboard() which returns reference to clipboard object. Any type of MimeData can be copied to or pasted from the clipboard.
Following are the clipboard class methods that are commonly used −
Sr.No. | Methods & Description |
---|---|
1 |
clear() Clears clipboard contents |
2 |
setImage() Copies QImage into clipboard |
3 |
setMimeData() Sets MIME data into clipboard |
4 |
setPixmap() Copies Pixmap object in clipboard |
5 |
setText() Copies QString in clipboard |
6 |
text() Retrieves text from clipboard |
Signal associated with clipboard object is −
Sr.No. | Method & Description |
---|---|
1 |
dataChanged() Whenever clipboard data changes |
Example
In the following example, two TextEdit objects and two Pushbuttons are added to a top level window.
To begin with the clipboard object is instantiated. Copy() method of textedit object copies the data onto the system clipboard. When the Paste button is clicked, it fetches the clipboard data and pastes it in other textedit object.
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): hbox = QVBoxLayout() self.edit1=QTextEdit() hbox.addWidget(self.edit1) self.btn1=QPushButton("Copy") hbox.addWidget(self.btn1) self.edit2=QTextEdit() self.btn2=QPushButton("Paste") hbox.addWidget(self.edit2) hbox.addWidget(self.btn2) self.btn1.clicked.connect(self.copytext) self.btn2.clicked.connect(self.pastetext) self.setLayout(hbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Clipboard') self.show() def copytext(self): #clipboard.setText(self.edit1.copy()) self.edit1.copy() print (clipboard.text()) msg=QMessageBox() msg.setText(clipboard.text()+" copied on clipboard") msg.exec_() def pastetext(self): self.edit2.setText(clipboard.text()) app = QApplication(sys.argv) clipboard=app.clipboard() ex = Example() ex.setWindowTitle("clipboard Example") sys.exit(app.exec_())
The above code produces the following output −