A few days ago, we went over copying any amount of text to the system clipboard using the system clipboard. However, you can also leverage the full-power of this by copying images and pixmaps to the system clipboard. Let’s get started!
Behold my stunning UI for this example. 😛 Now that you have your GUI setup, go to the push button’s slot (or wherever you want to put the code) and you are left with this:
QClipboard *p_Clipboard = QApplication::clipboard(); p_Clipboard->setPixmap(*(ui->label->pixmap()));
This is essentially what we did with text, however, with a different method. Note the dereference of the constant pointer we get to the label’s pixmap. This is essential, because the setPixmap() function wants a QPixmap object (taken by reference, not pointer). How can you not love C++? 🙂
Then you can paste it anywhere that lets you insert images through the paste!
And that about wraps it up. I hope you can use this in one of your projects, and share it if it was useful. Happy coding!