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
Contributor
ingeniumed
Posts: 16
Registered: ‎07-20-2012
My Device: Blackberry Playbook

Passing data from QML to C++

Hi,

 

I have a DateTime picker through which I wish to send my c++ function a QDateTime value, however everytime I say that in my code it crashes.

 

At this line:

editSheet.saveListItem(itemText.text, itemIndex.text, QDateTime startDate, QDateTime endDate)

 This causes my application hang and not go past the splash screen. 

 

signal saveListItem (string text, string index, string startDate, string endDate)

This is C function

 

void AppModel::setCalanderEvent (const QDateTime startDate, const QDateTime endDate, const QString title, const QString description)
{
// Create the calendar service object
CalendarService service;

// Create the calendar events
CalendarEvent reminder;

// Specify information for the first event
reminder.setStartTime(startDate);
reminder.setEndTime(endDate);
reminder.setSensitivity(Sensitivity::smileytongue:ersonal);
reminder.setAccountId(1);
reminder.setFolderId(1);
reminder.setSubject(title);
reminder.setBody(description);

// Add the event to the database
service.createEvent(reminder);
}

 

This function hasn't been called so far since the QML addition of QDateTime startTime and QDateTime endTime crashes the program. I have attached the QML code file below! Thanks :smileyhappy:

 

import bb.cascades 1.0
import bb.system 1.0

// sheet is used for editing and adding new items to the task list
Sheet {
    id: editSheet
    
    property alias title: addBar.title
    property alias hintText : itemText.hintText
    property alias text: itemText.text
    property alias index: itemIndex.text
    property alias flags: itemstartDate.visible
    property alias flage: itemendDate.visible
    
    //trigger signal once changes are accepted
    signal saveListItem (string text, string index)//, string startDate, string endDate)
    
    Page {
        id: addPage
        
        attachedObjects: [
            SystemToast {
                id: emptySave
                body: "Warning: Empty reminder saved!"
            }
        ]
        
        titleBar: TitleBar {
            id: addBar
            title: "Add"
            visibility: ChromeVisibility.Visible
            
            dismissAction: ActionItem {
                title: "Cancel"
                onTriggered: {
                    //hide sheet to indicate cancellation
                    editSheet.close();
                }
            }
            
            acceptAction: ActionItem {
                title: "Save"
                onTriggered: {
                    //hide sheet again to indicate finished but also trigger save signal
                    if (itemText.text==""){ 
                        if (itemIndex.text==""){
                            emptySave.show();
                        }
                    } else {
                        emptySave.body = "Reminder saved!";
                        emptySave.show();
                    }
                    editSheet.close();
                    editSheet.saveListItem(itemText.text, itemIndex.text, QDateTime startDate, QDateTime endDate)
                    emptySave.show();
                }
            }
        }
        
        Container {
            id: editC
            property real margins: 40
            background: backgroundPaint.imagePaint
            
            topPadding: editC.margins
            leftPadding: editC.margins
            rightPadding: editC.margins
            
            layout: DockLayout {
            }

            attachedObjects: [
                  TextStyleDefinition {
                      id: editIndexStyle
                      base: SystemDefaults.TextStyleStyles.TitleText
                      fontSize: FontSize.Medium
                  },
                  TextStyleDefinition {
                      id: editTextStyle
                      base: SystemDefaults.TextStyleStyles.BodyText
                      fontStyle: FontStyle.Italic
                      fontSize: FontSize.Medium
                  },
                  ImagePaintDefinition {
                      id: backgroundPaint
                      imageSource: "asset:///images/Tile_scribble_light_256x256.amd"
                      repeatPattern: RepeatPattern.XY
                  }
              ]
              
            Container {
                
                TextField {
                    id: itemIndex
                    hintText: "Title of the reminder"
                    topMargin: editC.margins
                    bottomMargin: topMargin
                    
                    textStyle {
                        base: editIndexStyle.style
                    }
                    
                    horizontalAlignment: HorizontalAlignment.Fill
                }
                
                TextArea {
                    id: itemText
                    hintText: "What's the reminder?"
                    topMargin: editC.margins
                    bottomMargin: topMargin
                    preferredHeight: 250
                    maxHeight: 250
                    textStyle {
                        base: editTextStyle.style
                    }
                    
                    horizontalAlignment: HorizontalAlignment.Fill
                }
                
                DateTimePicker {
                    id: itemstartDate
                    title: "Start Date"
                    mode: DateTimePickerMode.DateTime
                    value: { new Date(); }
                    topMargin: editC.margins
                    bottomMargin: topMargin
                }
                
                DateTimePicker {
                    id: itemendDate
                    title: "End Date"
                    mode: DateTimePickerMode.DateTime
                    value: { new Date(); }
                    topMargin: editC.margins
                    bottomMargin: topMargin
                }
            }
        }
    }
Developer
congdanhqx
Posts: 115
Registered: ‎10-22-2012
My Device: Developer

Re: Passing data from QML to C++

[ Edited ]

I see that you have this line of code:

signal saveListItem (string text, string index, string startDate, string endDate)

so try replace

editSheet.saveListItem(itemText.text, itemIndex.text, QDateTime startDate, QDateTime endDate)

with

editSheet.saveListItem(itemText.text, itemIndex.text, Qt.formatDateTime(startDate,'yyyy-MM-dd HH-mm-ss'), Qt.formatDateTime(endDate,'yyyy-MM-dd HH-mm-ss'))

or

signal saveListItem (string text, string index, string startDate, string endDate)

with

signal saveListItem (string text, string index, variant startDate, variant endDate)

and change

editSheet.saveListItem(itemText.text, itemIndex.text, startDate, endDate)

may be past.



Like my answer if it's helpful. Mark as answer if it help you solve the problem.
Contributor
ingeniumed
Posts: 16
Registered: ‎07-20-2012
My Device: Blackberry Playbook

Re: Passing data from QML to C++

Hmm, I had tried the first idea and it ended up giving a blank string back which was really wierd. Also is it possible in the second option to return a QDateTime type to the C function so that I don't have to deal with any conversions when passing it to the calendar service?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Passing data from QML to C++

you have to pass the values of the date time pickers.
to access them use the id, in your case "itemstartDate", with the "value" property.

editSheet.saveListItem(itemText.text, itemIndex.text,itemstartDate.value, itemendDate.value)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
ingeniumed
Posts: 16
Registered: ‎07-20-2012
My Device: Blackberry Playbook

Re: Passing data from QML to C++

Omg, how did I miss that. I was declaring a variable when I should instead be passing those values. Thanks for pointing that out! Was just curious that in the line where I have the signal defintion would I say QStartDate startDate?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Passing data from QML to C++

i would suggest to use date, as this is the basic qml type.
QDateTime is the type on the c++/Qt side.
see
https://qt-project.org/doc/qt-4.8/qdeclarativebasictypes.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
ingeniumed
Posts: 16
Registered: ‎07-20-2012
My Device: Blackberry Playbook

Re: Passing data from QML to C++

Ah I see what you mean, I can use two separate types to do this then: a date type and a time type then in the c++ side make a QDateTime object and set its values using the date time gotten in QML? Let me try it right now, thanks!
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Passing data from QML to C++

not sure about it, i don't have any signals defined in qml so far, i either define signals in c++ and connect them in qml, or call q_invokable methods.
if date as a type does not work you can either use variant or maybe use qmlRegisterType to define a custom type for the c++ type.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
ingeniumed
Posts: 16
Registered: ‎07-20-2012
My Device: Blackberry Playbook

Re: Passing data from QML to C++

Hmm I am curious about variant. If I pass a variant to my c++ function from QML, then how does accessing occur within the c++ function. For example if I pass variant startDate. Can i still go startDate.value and get a QDateTime type returned?
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Passing data from QML to C++

first, let me state that i am quite a bit outside my comfort zone, knowledge wise. see my posts as a mixture of guessing and some little experience, please :smileyhappy:

variant is a very variable type, basically QVariant i think, but with automatic wrapping and unwrapping. In your case, you put the return value of a DateTimePicker into it, which is a QDateTime according to the docs. If you pass it to c++ it should actually become a QDateTime object, without the QVariant around it.

I use variant for every type that is not one of the simple types i linked above.
So far the only limitation i have seen is that, if you want to pass a complex type from c++ to qml in a signal, you have to declare it as QObject* instead of its real type.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter