QClipboard wrapper for QML

Hi,

It’s not the first Qt class that i’m making usable in QML and will certainly not be the last one. Even better would be singleton components because you really don’t want two of these components in one application, something that is possible in Qt5 if i’m correct.

Note: It has been suggested that i make this as a plasma service. I do wonder, what’s the benefit over a component? And how to even make a service? Making this wrapper class is really easy, is it even easier with services? Making this wrapper was really easy and only took a few hours.

Without further delay, here is the “Clipboard” QML component free for you to use however you like.
clipboard.h

And the clipboard.cpp file:

You obviously have to register the class in QML as well. Look in my other posts if you don’t know how to do that.

Now using this class needs a bit of documentation. First, you have to add it to QML which is as simple as adding:

Lets say you want to paste the content of the clipboard. For that i take my Shortcut element as example, look at the following code:

This happens to be an example for CTRL+V (pasting). clip.paste is where the magic happens. All you have to do is provide a location (with the protocol in front of it like “file://”). Then the files that are on the clipboard will be put on that location.

If you want to copy/cut files you have to go through some more trouble. This “trouble” is there because QML doesn’t properly allow arrays to be filled after they have been defined in “property variant someArray”. This has been resolved in Qt 5 where “property var someArray” works just as you would expect from a javascript array.

So, in the above Clipboard QML component you have one function to fill the url: clip.addUrl(file:///your/url/to/anything/). If you have multiple urls you have to add them all – one by one.
clip.addUrl(file:///your/url/to/anything/one)
clip.addUrl(file:///your/url/to/anything/two)
clip.addUrl(file:///your/url/to/anything/three)

Another function you have is clip.urlList() which just spits out your url list. There is no remove/clear/anything else for the url container.
Once you’re done adding url entries you call clip.cut() or clip.copy(), depending on what you want to do. After that point the files (urls actually) will have been placed on the clipboard and the url list will be cleared ready for the next job.

That’s about all there is to know for this wrapper.

7 Thoughts on “QClipboard wrapper for QML

  1. Great!

    Why not contribute it to qt/qtdeclarative or playground/qtdesktopcomponents in https://codereview.qt-project.org/ ?

    • markg85 on December 28, 2012 at 8:56 PM said:

      Possible. I just didn’t spend any time on trying that yet. Though it seems a bit of a hassle to get things like that in. Just look at the current discussion for adding a QAction QML version. It’s a lot easier to make something, post it on my blog and “perhaps” add it to the kde plasma libs. If it gets used or is very useful then it can go in Qt itself as well.

  2. Getters like cutState() should be const.
    The slot’s name sounds more like a signal name. It should probably also be private unless you want QML to be able to invoke it.

    I am not sure you need the addUrl() work around, an exported QStringList property should work as well and be fillable with a JS array of string

    • markg85 on December 29, 2012 at 4:57 PM said:

      All true. I was actually wondering if i need to have a “cutState” at all. I think i manage just fine without it.

      As for addUrl. In QML you can either construct a javascript array (not as property ….) or an array as property using variant (property variant myArray: [1, 2, 3]). Then you can “replace” the variant content by replacing it with a javascript array and that will work. However, you can’t simply “append” alements to an array that you defined in the property system. You have to replace it. I figured that one out the hard way.. In Qt 5 it doesn’t work like that anymore and “property var myArray” works just like a javascript array where you can append values as expected. In Qt 4.8 i simply have to work around that issue.

      I can do your exact suggestion of filling a QStringList with a JS array, but that is more work then the approach i took now and a lot less efficient.

  3. I guess that having it as a Plasma service is something like having a singleton. This is just my guess, because I do not know anything about Plasma services, but we are using this concept (services as singletons) in our non-Qt application.

  4. Pingback: QClipboard wrapper for QML - Bartle Doo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation