2

カスケード QML から C++ 関数を呼び出したい

これは CalculatorQML.cpp です

// Default empty project template
#include "CalcolatorQML.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QDebug>

using namespace bb::cascades;

CalcolatorQML::CalcolatorQML(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);

// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);
//Container *container = root->findChild<Container*>("myContainer");
}
void CalcolatorQML::injectContainer(){
qDebug("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
}

Q_INVOKABLE void injectContainer(); そして、これがCalcolatorQML.hppであるため、関数を宣言します

// Default empty project template
#ifndef CalcolatorQML_HPP_
#define CalcolatorQML_HPP_

#include <QObject>

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 CalcolatorQML : public QObject
{
Q_OBJECT
public:
CalcolatorQML(bb::cascades::Application *app);
virtual ~CalcolatorQML() {}
Q_INVOKABLE void injectContainer();
};



#endif /* CalcolatorQML_HPP_ */

これは私のmain.qmlです。b1でこのステートメントをクリックするとわかりますinjection.injectContainer1();

import bb.cascades 1.0


Page {
content: Container {
    Container {
        layout: StackLayout {
        }
        TextField {
            id: textFieldId
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                id: b1
                text: "1"
                onClicked: {
                    injection.injectContainer();
                }
            }
            Button {
                id: b2
                text: "2"
            }
            Button {
                id: b3
                text: "3"
            }
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                id: b4
                text: "4"
            }
            Button {
                id: b5
                text: "5"
            }
            Button {
                id: b6
                text: "6"
            }
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                id: b7
                text: "7"
            }
            Button {
                id: b8
                text: "8"
            }
            Button {
                id: b9
                text: "9"
            }
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                id: b0
                text: "0"
            }
        }
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            Button {
                id: bPlus
                text: "+"
            }
            Button {
                id: bMult
                text: "*"
            }
            Button {
                id: bEqual
                text: "="
            }
            Button {
                id: bDivide
                text: "/"
            }
            Button {
                id: bMinus
                text: "-"
            }
        }
    }
}
}// end of Page

私の問題は関数injectContainerが呼び出されていないことですありがとう

4

2 に答える 2

4

CalcolatorQML.cppの行の

AbstractPane *root = qml->createRootObject<AbstractPane>(); 

追加:

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

次に、QMLのonClicked内で、次のことを試してください。

app.injectContainer();
于 2012-10-05T21:18:53.623 に答える
1

これを試してみてください

AbstractPane *root = qml->createRootObject<AbstractPane>(); 
qml->setContextProperty("root", this);

QmlonClickに書き込みます

root.injectContainer();
于 2012-10-25T07:56:50.420 に答える