2

我正在使用带有 C++ QT 和 QML 的 Blackberry Cascades 10 Beta 3 SDK 以及 Blackberry 10 Dev Alpha Simulator 和 QNX Momentics IDE,并且我正在尝试实现单击带有 QML 电话号码的按钮并拥有它的功能调出带有该号码的拨号盘,此时用户应该能够按下呼叫,它将呼叫该号码。以下是我做过的事情:

Button {
    text: "555-555-5555" //just a sample number - I don't actually use this number
    onClicked: Foo.phone(text) //I don't actually use the class name Foo
}

我有: ...

class Foo : public QObject {
    Q_OBJECT
public:
    ...
    Q_INVOKABLE void phone(QString number);
}
...

我有:

void Foo::phone(QString number) {
    bb::system::phone::Phone phone;
    phone.requestDialpad(number, bb::system::phone::LineType::Cellular);
}

但是当我点击按钮时它什么也没做——我需要显示拨号盘——有人知道我做错了什么吗?

4

1 回答 1

2

将类 Foo 的代码更改为以下内容:

Foo.hpp

#ifndef FOO_HPP_
#define FOO_HPP_

#include <QObject>
#include <bb/system/InvokeManager>

class Foo : public QObject {
    Q_OBJECT

public:
    Foo();
    virtual ~Foo();

    Q_INVOKABLE void callNumber(const QString& number);

private Q_SLOTS:
    void processInvokeReply(); // This slot handles the result of an invocation

private:
    bb::system::InvokeManager* _invokeManager;

    Q_DISABLE_COPY(Foo);
};
#endif /* FOO_HPP_ */

Foo.cpp:

#include <bb/system/InvokeAction>
#include <bb/system/InvokeReply>
#include <bb/system/InvokeTargetReply>
#include <bb/system/InvokeRequest>
#include <bb/PpsObject>
#include "Foo.hpp"

Foo::Foo() :
        _invokeManager(new InvokeManager(this)) {
}


Foo::~Foo() {
}

void Foo::processInvokeReply() {
    InvokeReply *reply = qobject_cast<InvokeReply*>(sender());     // Get the reply from the sender object

    // Check for errors during invocation
    switch (reply->error()) {
        case InvokeReplyError::BadRequest:
                qDebug("[ErrorBadRequest] Invoke Failed!");
            break;
        case InvokeReplyError::Internal:
                qDebug("[ErrorInternal] Invoke Failed!");
            break;
        case InvokeReplyError::NoTarget:
                qDebug("[ErrorNoTarget] Invoke Failed!");
            break;
        case InvokeReplyError::TargetNotOwned:
                qDebug("[ErrorTargetNotOwned] Invoke Failed.");
            break;
        default:
                qDebug("[Odd Error %d] Invoke failed", reply->error());
            break;
    }
    reply->deleteLater();     // Delete the reply later on
}


void Foo::callNumber(const QString& number) {
    QVariantMap map;
    map.insert("number", number);    // required
    QByteArray requestData = bb::PpsObject::encode(map, NULL);
    InvokeRequest request;
    request.setAction("bb.action.DIAL");
    request.setData(requestData);
    request.setMimeType("application/vnd.blackberry.phone.startcall");
    const InvokeTargetReply *reply = _invokeManager->invoke(request);
    if (reply) {
        QObject::connect(reply, SIGNAL(finished()), this, SLOT(processInvokeReply()));
    } else {
        qWarning() << "Invoke Failed! InvokeReply is empty.";
    }
}

通过启动应用程序的初始化 cpp 代码公开它:

Foo* _foo = new Foo();
qml->setContextProperty("_foo", _foo);

然后像这样在 QML 中使用它:

Button {
    onClicked: {
        _foo.callNumber("555-555-5555")
    }
}

添加:

此外,还有一种更简单的方法可以做到这一点:

在 main.cpp 中:

#include <bb/system/phone/Phone>
#include <bb/data/DataSource>

// skipped ...

Q_DECL_EXPORT int main(int argc, char **argv)
{
     // ...skipped 
        qmlRegisterType<bb::system::phone::Phone>("bb.system.phone", 1, 0, "Phone");
        bb::data::DataSource::registerQmlTypes();
     // ...skipped
}

然后在 QML 文件中:

import bb.cascades 1.0
import bb.system.phone 1.0

// Creates one page with a button. When you tap the button,
// a dial pad with a specific phone number is displayed.

Page {
    Container {
        layout: DockLayout {
        }
        verticalAlignment: VerticalAlignment.Center
        horizontalAlignment: HorizontalAlignment.Center

        Button {
            id: callButton
            text: "Call me, maybe"

            onClicked: {
                phone.requestDialpad("(519) 555-0100")
            }
        }
    }
    attachedObjects: [
        Phone {
            id: phone
        }
    ]
}

在此处阅读有关此示例的更多信息 - http://developer.blackberry.com/cascades/documentation/device_comm/phone/index.html

于 2013-02-25T00:26:38.880 回答