简体   繁体   中英

Linux and clipboard

In Linux after text selecting it copies to buffer, so than we could paste it by clicking middle button of the mouse. I think that there is a special buffer for this thing. I want to use it. How could i get data of selected text?

  • OS: Linux
  • Programming language: c++
  • Own libraries: Qt

Thanks.

Just a more accurate answer than Paul Dixon's that answers your needs:

QClipboard* clipboard = QApplication::clipboard();
QString selectedText = clipboard->text(QClipboard::Selection);

You need to distinguish between selection and clipboard. The QClipboard documentation has this in the Notes for X11 Users section:

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.

With QClipboard::Mode you can select which type (clipboard or selection) you want to access. The important part is that you need to be aware about the difference between selection and clipboard.

If you're using Qt, have you read the fine manual page on QClipboard ?

QClipboard *clipboard = QApplication::clipboard();
QString clipboardText = clipboard->text();

the system that actually handles the selection and pasting system is X11 Windows. When you eg paint some text in your favorite editor, the application sends and X11 request which tells to the X11 server that you have an active selection. If you then click the middle mouse button somewhere, the X11 server queries the application which told the server about the selection for the actual contents. Then the contents are forwarded to the receiving application.

Libraries like Qt provide wrappers for this mechanism, but the underlying mechanism is X11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM