2

我想在 IMSI 上显示/阅读(在 Dev Alpha B 上测试过)。问题是我可以读取 SimCardInfo 的其他属性,例如移动网络代码、移动国家代码、序列号,但 IMSI(订阅者标识符)。这是代码

主文件

#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 _ _

4

1 回答 1

1

您声明hwInfosimCardInfo作为堆栈变量,这意味着在构造函数结束后两个变量都不再存在,因此当 QML 运行时尝试访问您分配给它们的属性时,它就是不能。

要使其工作,您需要将这些变量声明为堆变量,以便它们可以超出其范围。

HardwareInfo* hwInfo = new HardwareInfo();
SimCardInfo* simcardInfo = new SimCardInfo();
qml->setContextProperty("_hardware", hwInfo);
qml->setContextProperty("_simcardinfo", simcardInfo);
于 2015-01-26T20:19:14.157 回答