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
Highlighted
Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none
Accepted Solution

How to pass a QString from QML to C++

[ Edited ]

Hello,

 

how can I pass a QString from QML to C++ and add it to a list view. Here's the code describing my problem.

 

I have a mainpage containing a list view:

// applicationui.cpp
[...]
void ApplicationUI::initFullUI() { QmlDocument *qml = QmlDocument::create("asset:///Main.qml").parent(this); qml->setContextProperty("app", this); // Create root object for the UI AbstractPane *root = qml->createRootObject<AbstractPane>(); ArrayDataModel *model = new ArrayDataModel;

if(------------->*\ file selected from qml /*<---------------){ model->append(------------->*\String from QML/*<---------------); }
// Connect to list view and update modal ListView *listView = root->findChild<ListView*>("listview"); listView->setDataModel(model); Application::instance()->setScene(root); }

 The string I want to give to C++ comes from the filepicker on the main.qml

// main.qml
[...]
attachedObjects: [ FilePicker { id: filePicker property string selectedFile mode: FilePickerMode.Picker type: FileType.Other title: "Open file..." viewMode: FilePickerViewMode.Default sortBy: FilePickerSortFlag.Default defaultType: FileType.Other onFileSelected: { console.log("FileSelected signal recieved : " + selectedFiles); selectedFile = selectedFiles[0]; } } ]

 Now I made C++ functions to save the string:

// Document.hpp
class Document: public QObject {
Q_PROPERTY(QString document READ document WRITE setDocument NOTIFY documentChanged)

public:
Q_INVOKABLE QString document();
Q_INVOKABLE void setDocument(QString &document);

private:
QString m_document;
}
// Document.cpp

QString Document::document(){ return m_document; }

void Document::setDocument(const QString &document){
    if(document != m_document) {
            m_document = document;
            emit documentChanged(m_document);
        }
}

Is it possible with this way to fill the string into the list view? Also how can I make C++ know that the a file was selected from the filepicker so that it can add a new element to the list?

 

Or should I go another way (e.g. by writing also the filepicker entirely in C++)?

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to pass a QString from QML to C++

There's not a lot of point going through C++ when both of your conponants are in the QML file just add it directy within the QML file.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to pass a QString from QML to C++

Also when setting a property you don't have to declare the property linked functions as Q_INVOKABLE.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to pass a QString from QML to C++

Here's an example it uses a button rather then the FilePicker but the mechanism is the same...

 

Button {
    property int count: 0
    text: "Add item"
    onClicked: {
        count++
        theDataModel.append("button click " + count)
    }
}

ListView {
    dataModel: ArrayDataModel {
        id: theDataModel
    }

    [ ... ]
}

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none

Re: How to pass a QString from QML to C++

Thanks that works

 

But how do I get the information let's say as title of the new added listitem.

 

ListView {
    dataModel: ArrayDataModel {
        id: theDataModel
    }

    listItemComponents: [
                    ListItemComponent {

                        StandardListItem {


                            title: theDataModel.???????
                            description: "description"
                            imageSource: "asset:///images/icon.png" }

 

Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none

Re: How to pass a QString from QML to C++

okay I get it with

title: listItemData.toString()

 I should try a little longer before posting a question

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to pass a QString from QML to C++

You can place anything in to an Array it doesn't have to be a single string therefore you could for instance insert a Map, any QVariant type is fine, then retrieve the Map and set the various Listitem elements accordingly.

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to pass a QString from QML to C++

For a single string you shouldn't need the toString() part just ListItemData would do.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none

Re: How to pass a QString from QML to C++

Yes you're right.