10-25-2012 12:36 AM
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
Solved! Go to Solution.
10-25-2012 04:53 AM - edited 10-25-2012 04:54 AM
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(th is);
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);
}
}
10-27-2012 12:24 AM - edited 10-27-2012 02:38 AM
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
10-27-2012 02:18 AM
10-27-2012 02:44 AM - edited 10-27-2012 02:46 AM
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
10-27-2012 03:27 AM
10-27-2012 04:18 AM
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
10-27-2012 02:08 PM - edited 10-27-2012 10:49 PM
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(th is);
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);
}
10-28-2012 02:58 PM
Any pointers, tips, suggestions? from other examples i followed it "should" work yet it still doesnt
10-29-2012 10:48 AM