Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus
Accepted Solution

Clipboard

I would like to set a QML Button to copy text from my QML TextArea but Qclipboard is not supported which means it has to be done in C++

 

What i'm not understanding is how to tell my C++ clipboard that i want the text from QML TextArea when the button is clicked

Please use plain text.
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: Clipboard

[ Edited ]

One way to achieve this: 1. in c++, you can set any object as context into the qml, e.g.

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("myObject", objectOfSomeClass);

 Then in the class that is set, you can define Q_INVOKABLE methods, You can do anything you want (including put the text into clipboard) in the implementation. e.g.

Q_INVOKABLE void myButtonClicked(QString text);

 Finally, you can use java script in QML to trigger the "myButtonClicked()", e.g.

Button {
    text: qsTr("Login")
    onClicked: {
        myObject.myButtonClicked(myTextArea.text);
    }
}

 

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Clipboard

[ Edited ]

something i'm not getting is 

 

void MyAppName::copyButtonClicked(QString text)
{

	 Clipboard clipboard;
	 clipboard.clear();
	 clipboard.insert(text, "text/plain");
}

 I've managed to make it clear the clipboard after some playing around with what you gave me (thanks alot btw) the only other thing I don't get is how do i make it get the text from myTextArea i assume that's what QString text is but when this is run my copy button only clears the text leading me to belive i havent properly conneted it to the text area... i'll put everything i have below

 

set object into context

qml->setContextProperty("clipboard", this);

 set as public under myappname();

Q_INVOKABLE void copyButtonClicked(QString text);

 

 

Button {
 onClicked: {
      clipboard.copyButtonClicked(myTextArea.text);
   }
}

 do i have to declare myTextArea like i did the clipboard & button in c++ for it to copy the text from it

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Clipboard

Where and how is your text area defined? Does it have an ID of myTextArea?

Are you setting the context property before creating the rootobject? (its necessary). You have myButtonClicked and copyButtonClicked, which one is it trying to call?
Like all of my posts
Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Clipboard

[ Edited ]

mybuttonclicked was typo**

textArea is defined in QML, it has an id & an objectName (i've tried setting each as myTextArea)

the "clipboard" context property was set before the root object
it appears everything works except inputing text from myTextArea
because when the copy button is pressed the clipboard is cleared but no text is inserted

 

... dont know if it matters but rootobject is appPage, there is also something already using it and another context property set however everythings working except insert

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Clipboard

Have you put a break point in copyButtonClicked to confirm that you actually have text coming in there? Or a breakpoint?
Like all of my posts
Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Clipboard

haven't debugged it, only ran (which does everything except insert text)... doing that it gets stuck on the line after int main in main.cpp

 

clipboard.copyButtonClicked(myTextArea.text);


 i noticed i get a little info to the side because of clipboard, says unknown symbol 

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Clipboard

[ Edited ]

Sorry about all that earlier... i re-wrote it all like below and it still doesnt insert any text, yet clears clipboard text I don't see what i've done wrong

 

set context

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("app", this);

 from header

Q_INVOKABLE

    void copyButtonClicked(QString text);

 the "logic" & problem area

void Tumblr::copyButtonClicked(QString text)
{

	Clipboard clipboard;
	clipboard.clear();
	clipboard.insert(text, "text/plain");

}

 everything until clipboard.insert(); works like a charm, clipboard.insert("not sure what to do here"); does nothing.....

 

what i'm not understanding is how i'm telling it what text to insert from where since as/is doesnt work

 

button

onClicked: {
                app.copyButtonClicked(in1.text);
            }

 

Please use plain text.
Developer
slashkyle
Posts: 814
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Clipboard

Any pointers, tips, suggestions?  from other examples i followed it "should" work yet it still doesnt

Please use plain text.
Developer
contryboy
Posts: 68
Registered: ‎08-16-2012
My Device: BB10 Dev Alpha
My Carrier: NA

Re: Clipboard

I would suggest to debug (either step by step or just print debug info ) to see if the text passed in copyButtonClicked actually contains the text. This will narrow down the issue.
Please use plain text.