CopperSpice API  1.9.1
QClipboard Class Reference

The QClipboard class provides access to the window system clipboard. More...

Inheritance diagram for QClipboard:
QObject

Public Types

enum  Mode
 

Public Signals

void changed (QClipboard::Mode mode)
 
void dataChanged ()
 
void findBufferChanged ()
 
void selectionChanged ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

void clear (Mode mode=Clipboard)
 
QImage image (Mode mode=Clipboard) const
 
const QMimeDatamimeData (Mode mode=Clipboard) const
 
bool ownsClipboard () const
 
bool ownsFindBuffer () const
 
bool ownsSelection () const
 
QPixmap pixmap (Mode mode=Clipboard) const
 
void setImage (const QImage &image, Mode mode=Clipboard)
 
void setMimeData (QMimeData *data, Mode mode=Clipboard)
 
void setPixmap (const QPixmap &pixmap, Mode mode=Clipboard)
 
void setText (const QString &text, Mode mode=Clipboard)
 
bool supportsFindBuffer () const
 
bool supportsSelection () const
 
QString text (Mode mode=Clipboard) const
 
QString text (QString &subtype, Mode mode=Clipboard) const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Friends

class QApplication
 
class QPlatformClipboard
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QClipboard class provides access to the window system clipboard.

The clipboard offers a simple mechanism to copy and paste data between applications.QClipboard supports the same data types that QDrag does, and uses similar mechanisms. For advanced clipboard usage read Drag and Drop. There is a single QClipboard object in an application, accessible as QApplication::clipboard().

QString originalText = clipboard->text();
// do something
clipboard->setText(newText);

QClipboard has several methods to access common data types: setText() allows the exchange of Unicode text and setPixmap() and setImage() allows the exchange of QPixmaps and QImages between applications. The setMimeData() function is the ultimate in flexibility: it allows you to add any QMimeData into the clipboard. There are corresponding getters for each of these, e.g. text(), image() and pixmap(). You can clear the clipboard by calling clear().

A typical example is shown below.

void DropArea::paste()
{
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
if (mimeData->hasImage()) {
setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
} else if (mimeData->hasHtml()) {
setTextFormat(Qt::RichText);
} else if (mimeData->hasText()) {
setTextFormat(Qt::PlainText);
} else {
setText(tr("Unable to display data"));
}

Notes for X11 Users

  • The X11 Window System has the concept of a separate selection and clipboard. When text is selected, it is immediately available as the global mouse selection. The global mouse selection may later be copied to the clipboard. By convention, the middle mouse button is used to paste the global mouse selection.
  • X11 also has the concept of ownership; if you change the selection within a window, X11 will only notify the owner and the previous owner of the change, i.e. it will not notify all applications that the selection or clipboard data changed.
  • Lastly, the X11 clipboard is event driven, i.e. the clipboard will not function properly if the event loop is not running. Similarly, it is recommended that the contents of the clipboard are stored or retrieved in direct response to user-input events, e.g. mouse button or key presses and releases. You should not store or retrieve the clipboard contents in response to timer or non-user-input events.
  • Since there is no standard way to copy and paste files between applications on X11, various MIME types and conventions are currently in use. For instance, Nautilus expects files to be supplied with a x-special/gnome-copied-files MIME type with data beginning with the cut/copy action, a newline character, and the URL of the file.

Notes for Mac OS X Users

Mac OS X supports a separate find buffer that holds the current search string in Find operations. This find clipboard can be accessed by specifying the FindBuffer mode.

Windows and Mac OS X Users

  • Windows and Mac OS X do not support the global mouse selection, they only support the global clipboard, this means they only add text to the clipboard when an explicit copy or cut is made.
  • Windows and Mac OS X does not have the concept of ownership, the clipboard is a fully global resource so all applications are notified of changes.
See also
QApplication

Member Enumeration Documentation

This enum type is used to control which part of the system clipboard is used by QClipboard::mimeData(), QClipboard::setMimeData() and related functions.

ConstantValueDescription
QClipboard::Clipboard0indicates that data should be stored and retrieved from the global clipboard.
QClipboard::Selection1indicates that data should be stored and retrieved from the global mouse selection. Support for Selection is provided only on systems with a global mouse selection (e.g. X11).
QClipboard::FindBuffer2indicates that data should be stored and retrieved from the Find buffer. This mode is used for holding search strings on Mac OS X.
See also
QClipboard::supportsSelection()

Method Documentation

void QClipboard::changed ( QClipboard::Mode  mode)
signal

This signal is emitted when the data for the given clipboard mode is changed.

See also
dataChanged(), selectionChanged(), findBufferChanged()
void QClipboard::clear ( Mode  mode = Clipboard)

Clear the clipboard contents.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, this function clears the global clipboard contents. If mode is QClipboard::Selection, this function clears the global mouse selection contents. If mode is QClipboard::FindBuffer, this function clears the search string buffer.

See also
QClipboard::Mode, supportsSelection()
void QClipboard::dataChanged ( )
signal

This signal is emitted when the clipboard data is changed.

Clipboard changes made by other applications will only be detected when the application is activated.

See also
findBufferChanged(), selectionChanged(), changed()
void QClipboard::findBufferChanged ( )
signal

This signal is emitted when the find buffer is changed. This only applies to Mac OS X.

Clipboard changes made by other applications will only be detected when the application is activated.

See also
dataChanged(), selectionChanged(), changed()
QImage QClipboard::image ( Mode  mode = Clipboard) const

Returns the clipboard image, or returns a null image if the clipboard does not contain an image or if it contains an image in an unsupported image format.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the image is retrieved from the global clipboard. If mode is QClipboard::Selection, the image is retrieved from the global mouse selection.

See also
setImage(), pixmap(), mimeData(), QImage::isNull()
const QMimeData * QClipboard::mimeData ( Mode  mode = Clipboard) const

Returns a reference to a QMimeData representation of the current clipboard data.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the data is retrieved from the global clipboard. If mode is QClipboard::Selection, the data is retrieved from the global mouse selection. If mode is QClipboard::FindBuffer, the data is retrieved from the search string buffer.

The text(), image(), and pixmap() functions are simpler wrappers for retrieving text, image, and pixmap data.

See also
setMimeData()
bool QClipboard::ownsClipboard ( ) const

Returns true if this clipboard object owns the clipboard data, otherwise returns false.

bool QClipboard::ownsFindBuffer ( ) const

Returns true if this clipboard object owns the find buffer data, otherwise returns false.

bool QClipboard::ownsSelection ( ) const

Returns true if this clipboard object owns the mouse selection data, otherwise returns false.

QPixmap QClipboard::pixmap ( Mode  mode = Clipboard) const

Returns the clipboard pixmap, or null if the clipboard does not contain a pixmap. Note that this can lose information. For example, if the image is 24-bit and the display is 8-bit, the result is converted to 8 bits, and if the image has an alpha channel, the result just has a mask.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the pixmap is retrieved from the global clipboard. If mode is QClipboard::Selection, the pixmap is retrieved from the global mouse selection.

See also
setPixmap(), image(), mimeData(), QPixmap::convertFromImage()
void QClipboard::selectionChanged ( )
signal

This signal is emitted when the selection is changed. This only applies to windowing systems that support selections, e.g. X11. Windows and Mac OS X do not support selections.

See also
dataChanged(), findBufferChanged(), changed()
void QClipboard::setImage ( const QImage image,
Mode  mode = Clipboard 
)

Copies the image into the clipboard.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the image is stored in the global clipboard. If mode is QClipboard::Selection, the data is stored in the global mouse selection.

This is shorthand for:

QMimeData *data = new QMimeData;
data->setImageData(image);
clipboard->setMimeData(data, mode);
See also
image(), setPixmap(), setMimeData()
void QClipboard::setMimeData ( QMimeData data,
Mode  mode = Clipboard 
)

Sets the clipboard data to data. Ownership of the data is transferred to the clipboard. If you want to remove the data either call clear() or call setMimeData() again with new data.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the data is stored in the global clipboard. If mode is QClipboard::Selection, the data is stored in the global mouse selection. If mode is QClipboard::FindBuffer, the data is stored in the search string buffer.

The setText(), setImage() and setPixmap() functions are simpler wrappers for setting text, image and pixmap data respectively.

See also
mimeData()
void QClipboard::setPixmap ( const QPixmap pixmap,
Mode  mode = Clipboard 
)

Copies pixmap into the clipboard. Note that this is slower than setImage() because it needs to convert the QPixmap to a QImage first.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the pixmap is stored in the global clipboard. If mode is QClipboard::Selection, the pixmap is stored in the global mouse selection.

See also
pixmap(), setImage(), setMimeData()
void QClipboard::setText ( const QString text,
Mode  mode = Clipboard 
)

Copies text into the clipboard as plain text. The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the text is stored in the global clipboard. If mode is QClipboard::Selection, the text is stored in the global mouse selection. If mode is QClipboard::FindBuffer, the text is stored in the search string buffer.

See also
text(), setMimeData()
bool QClipboard::supportsFindBuffer ( ) const

Returns true if the clipboard supports a separate search buffer, otherwise returns false.

bool QClipboard::supportsSelection ( ) const

Returns true if the clipboard supports mouse selection, otherwise returns false.

QString QClipboard::text ( Mode  mode = Clipboard) const

Returns the clipboard text as plain text, or an empty string if the clipboard does not contain any text.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the text is retrieved from the global clipboard. If mode is QClipboard::Selection, the text is retrieved from the global mouse selection. If mode is QClipboard::FindBuffer, the text is retrieved from the search string buffer.

See also
setText(), mimeData()
QString QClipboard::text ( QString subtype,
Mode  mode = Clipboard 
) const

Returns the contents of the clipboard for the given subtype or an empty string if the clipboard does not contain text of the request subtype. Common values for subtype are "plain" or "html". If this specified subtype is empty then text for any subtype will be returned. In this case the subtype parameter will be updated to reflect the subtype which was used.

The mode argument is used to control which part of the system clipboard is used. If mode is QClipboard::Clipboard, the text is retrieved from the global clipboard. If mode is QClipboard::Selection, the text is retrieved from the global mouse selection.

Calling this method repeatedly may be slow. In such cases, you should use the dataChanged() signal instead.

See also
setText(), mimeData()