English 中文(简体)
PyQt5 - QClipboard
  • 时间:2024-11-03

PyQt5 - QCppboard


Previous Page Next Page  

The QCppboard class provides access to system-wide cppboard that offers a simple mechanism to copy and paste data between apppcations. Its action is similar to QDrag class and uses similar data types.

QApppcation class has a static method cppboard() which returns reference to cppboard object. Any type of MimeData can be copied to or pasted from the cppboard.

Following are the cppboard class methods that are commonly used −

Sr.No. Methods & Description
1

clear()

Clears cppboard contents

2

setImage()

Copies QImage into cppboard

3

setMimeData()

Sets MIME data into cppboard

4

setPixmap()

Copies Pixmap object in cppboard

5

setText()

Copies QString in cppboard

6

text()

Retrieves text from cppboard

Signal associated with cppboard object is −

Sr.No. Method & Description
1

dataChanged()

Whenever cppboard data changes

Example

In the following example, two TextEdit objects and two Pushbuttons are added to a top level window.

To begin with the cppboard object is instantiated. Copy() method of textedit object copies the data onto the system cppboard. When the Paste button is cpcked, it fetches the cppboard 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.cpcked.connect(self.copytext)
      self.btn2.cpcked.connect(self.pastetext)
      self.setLayout(hbox)
      
      self.setGeometry(300, 300, 300, 200)
      self.setWindowTitle( Cppboard )
      self.show()
      
   def copytext(self):

      #cppboard.setText(self.edit1.copy())
      self.edit1.copy()
      print (cppboard.text())

      msg=QMessageBox()
      msg.setText(cppboard.text()+" copied on cppboard")
      msg.exec_()

   def pastetext(self):
      self.edit2.setText(cppboard.text())

app = QApppcation(sys.argv)
cppboard=app.cppboard()
ex = Example()
ex.setWindowTitle("cppboard Example")
sys.exit(app.exec_())

The above code produces the following output −

QCppboard Advertisements