Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By travisredpath
  1. DisturbedRocks31's Avatar
    CrackBerry Addict

    Posts
    552 Posts
    Global Posts
    554 Global Posts
    Thread AuthorThread Author   #1  

    Default Calling a C++ function from QML

    I have never developed an app before and this is my first time, I've decided to go with BB10 Native cascades.

    Anyways, I'm trying to call a C++ function from the main.QML file when a button is pressed. But it's not working!

    I'm not sure what the problem is, I can get the button to do random stuff like show w.e. is in the input field but it doesn't call the C++ function.... It's also showing a warning: "Unknown Symbol 'HelloWorldBB10'" but I have no idea how to fix it. I thought adding the
    Code:
    qml->setContextProperty("HelloWorldBB10", this);
    would fix that but it doesn't.

    Anyone know what I'm not doing?

    Relevant
    Code:
    DropDown {
                id: direction
                objectName: "direction"
                title : "Converting to ..."
                enabled : true
                topMargin: 50.0
                onSelectedIndexChanged : {
                    console.log("SelectedIndex was changed to " + selectedIndex);
                }
    
                // text + description
                horizontalAlignment: HorizontalAlignment.Center
                Option {
                     text : "Numbers"
                     description : "English To NUmbers"
                     value : "r"
                     selected : true
                 }
                 Option {
                     text : "English"
                     description : "Numbers To English"
                     value : "e"
                 }
    		 }
    
     TextArea {
                id: inputText
                objectName: "inputText"
                hintText: "Enter or Paste Your Input Here!"
                inputMode: TextAreaInputMode.Default
                editable: true
                focusPolicy: FocusPolicy.Default
            }
    
            Button {
                id: convertButton
                objectName: "convertButton"
                text: "Convert"
                onClicked: {
                    output.text = HelloWorldBB10.convert(inputText.text, direction.selectedValue);
                }
            }
    
            Label {
                id: output
                objectName: "output"
                text: ""
            }
    Then, my HelloWorldBB10.hpp file is:
    Code:
    // Default empty project template
    #ifndef HelloWorldBB10_HPP_
    #define HelloWorldBB10_HPP_
    
    #include <QtCore/QObject>
    #include <QtCore/QMetaType>
    #include <bb/cascades/Event>
    #include <bb/cascades/UiObject>
    #include <bb/cascades/Control>
    #include <QObject>
    #include <string.h>
    
    namespace bb { namespace cascades { class Application; }}
    
    /*!
     * @brief Application pane object
     *
     *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
     */
    class HelloWorldBB10 : public QObject
    {
        Q_OBJECT
    public:
        HelloWorldBB10(bb::cascades::Application *app);
    
        Q_INVOKABLE std::string convert(std::string input, char direction);
    
        virtual ~HelloWorldBB10() {}
    };
    
    
    #endif /* HelloWorldBB10_HPP_ */
    and finally my HelloWorldBB10.cpp is:

    Code:
    HelloWorldBB10::HelloWorldBB10(bb::cascades::Application *app)
    : QObject(app)
    {
        // create scene document from main.qml asset
        // set parent to created document to ensure it exists for the whole application lifetime
        QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
        qml->setContextProperty("HelloWorldBB10", this);
        // create root object for the UI
        AbstractPane *root = qml->createRootObject<AbstractPane>();
        // set created root object as a scene
        app->setScene(root);
    }
    
    
    std::string HelloWorldBB10::convert(std::string input, char direction)
    {
    //function left out...
    return input;
    }
    Last edited by JameB; 12-25-2012 at 01:07 AM.
  2. travisredpath's Avatar
    CrackBerry Abuser

    Posts
    188 Posts
    Global Posts
    190 Global Posts
    #2  

    Default

    I think the problem is that you're trying to use std::string in the Qt world, change them to QString and see if that fixes it. I think the char should work because it's just a short integer but if you still have problems there is also a QChar but you probably want an int.
    Thanked by:
    DisturbedRocks31 (12-25-2012) 
    DisturbedRocks31 likes this.
  3. DisturbedRocks31's Avatar
    CrackBerry Addict

    Posts
    552 Posts
    Global Posts
    554 Global Posts
    Thread AuthorThread Author   #3  

    Default

    Thanks A Lot! That was spot on.

Similar Threads

  1. Call Blocker now available from Mobihand
    By EpicDraws in forum BlackBerry OS Apps
    Replies: 28
    Last Post: 05-13-2009, 07:42 AM
  2. Replies: 5
    Last Post: 05-08-2009, 07:57 AM
  3. "Search" Functionality from Calendar
    By brucem76 in forum General BlackBerry Discussion
    Replies: 2
    Last Post: 04-08-2009, 06:10 AM
  4. How do I access call logs and textmessage from my computer?
    By tomrey in forum BlackBerry Curve 83xx
    Replies: 16
    Last Post: 04-06-2009, 09:17 AM
  5. Called my Favorite rep from Verizon Wireless in DC today..
    By ClassaN0va in forum BlackBerry Storm
    Replies: 0
    Last Post: 11-21-2008, 10:12 AM

Posting Permissions