2013-03-01 23 views
2

我想顯示/閱讀IMSI(在Dev Alpha B上測試過)。問題是我可以讀取SimCardInfo的其他屬性,例如移動網絡代碼,移動國家代碼,序列號碼,但IMSI(subscriberIdentifier)。 下面是代碼Blackberry10上的SimCardInfo問題

的main.cpp

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 
#include <bb/device/HardwareInfo> 
#include <bb/device/SimCardInfo> 

#include <QLocale> 
#include <QTranslator> 
#include <Qt/qdeclarativedebug.h> 

using namespace bb::cascades; 
using namespace bb::device; 

Q_DECL_EXPORT int main(int argc, char **argv) 
{ 
    qmlRegisterUncreatableType<bb::device::HardwareInfo>("bb.device", 1, 0, "HardwareInfo", ""); 
    qmlRegisterUncreatableType<bb::device::SimCardInfo>("bb.device", 1, 0, "SimCardInfo", ""); 

    // this is where the server is started etc 
    Application app(argc, argv); 

    // localization support 
    QTranslator translator; 
    QString locale_string = QLocale().name(); 
    QString filename = QString("hwinfo_%1").arg(locale_string); 
    if (translator.load(filename, "app/native/qm")) { 
     app.installTranslator(&translator); 
    } 

    //create object 
    HardwareInfo hwInfo; 
    SimCardInfo simcardInfo; 

    QmlDocument *qml = QmlDocument::create("asset:///main.qml"); 

    qml->setContextProperty("_hardware", &hwInfo); 
    qml->setContextProperty("_simcardinfo", &simcardInfo); 


    // create root object for the UI 
    AbstractPane *root = qml->createRootObject<AbstractPane>(); 
    // set created root object as a scene 
    Application::instance()->setScene(root); 


    // we complete the transaction started in the app constructor and start the client event loop here 
    return Application::exec(); 
    // when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children) 
} 

main.qml文件

import bb.cascades 1.0 
import bb.device 1.0 

Page 
{ 
    Container 
    { 
     leftPadding: 20 
     topPadding: 20 
     Container 
     { 
      topMargin: 10 
      layout: StackLayout 
        { 
         orientation: LayoutOrientation.LeftToRight 
        } 
      Button 
      { 
       text: "retrieve" 
       onClicked: 
       { 
        lbl0.text = "Model name: " + _hardware.modelName 
        lbl1.text = "IMEI: " + _hardware.imei 
        lbl2.text = "IMSI: " + _simcardinfo.subscriberIdentifier 
        lbl3.text = "SN: " + _simcardinfo.serialNumber 
        lbl4.text = "Mobile Network Code: " + _simcardinfo.mobileNetworkCode 
        lbl5.text = "Mobile Country Code: " + _simcardinfo.mobileCountryCode 
       } 
      } 
     } 

     Container 
     { 
      layout: StackLayout {       
        } 
      Label 
      { 
       id:lbl0 
      }      
      Label 
      { 
       id:lbl1  
      } 
      Label 
      { 
       id:lbl2 
      }   
      Label 
      { 
       id:lbl3 
      }   
      Label 
      { 
       id:lbl4 
      }   
      Label 
      { 
       id:lbl5 
      }  
     } 

    } 
} 

任何幫助深表感謝。

參考 http://developer.blackberry.com/cascades/reference/bb_device_simcardinfo.html

回答

1

你聲明hwInfosimCardInfo爲堆棧變量,這意味着構造函數結束後,這兩個變量不再存在,所以當QML運行試圖訪問你分配給他們的Properties只是不能。

爲了使它工作,您需要將這些變量聲明爲堆變量,以便它們可以超出其範圍。

HardwareInfo* hwInfo = new HardwareInfo(); 
SimCardInfo* simcardInfo = new SimCardInfo(); 
qml->setContextProperty("_hardware", hwInfo); 
qml->setContextProperty("_simcardinfo", simcardInfo);