2012-11-05 15 views
2

我正在使用黑莓級聯10 Beta 3 SDK與C++ QT & QML與黑莓10 Dev Alpha模擬器和QNX Momentics IDE,我試圖實現能夠從QML中點擊帶有電話號碼的按鈕,然後使用該號碼撥打該撥號盤,此時用戶應該能夠按通話並撥打該號碼。以下是諸如此類的事情,我已經做了:如何在Blackberry Cascades中顯示具有一定數量的手機板

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); 
} 

但是,當我點擊按鈕它什麼也不做 - 我需要撥號盤才能顯示 - 有沒有人知道我做錯了什麼?

+0

如果你想知道 - 在同一類的其他Q_INVOKABLE成員函數的手機是在 - 來自同一個QML文件調用的電話是在 - 所以這不是一個問題該類不是通過某個特定的變量註冊到該特定的qml文檔,也不是通過使用'qmlRegisterType '來使用正確的變量名稱來引用來自onClicked事件處理程序中的qml的該類的問題 – user1296259

+0

' Foo類可用於QML嗎? – donturner

+0

是的,我正在使用qmlRegisterType user1296259

回答

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

相關問題