×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    Unhappy How to load the qml file in C++ dynamically?

    Dear friends, I met a question during load a qml file in the C++,can u find the problem?

    My Purpose:
    I have two qml files:main.qml,second.qml. When the main.qml is shown, a Enter key will lead to load the second.qml.
    My Solutions
    Steps:
    1. Create a Qt Project with the Qt-Creator,which produces a main.qml automaliy;
    Code:
    import QtQuick 1.0
    Rectangle {
        id: screen
        width: 1100; height: 520
        Text{
             text : "First.qml"
        }
        Keys.onPressed: {
            console.log("Key_Value = " + event.key + ";Key_enter = " + Qt.Key_Enter);
            if (event.key + 1 == Qt.Key_Enter){
                console.log("Key_enter is pressed");
                CWinManager.pushWindow("second.qml");
                screen.focus = true;
    
            }
        }
    }
    2. Define a Class CWinManager which inherits from QDeclarativeItem, in the class define a function which can be called in qml by the modification:Q_INVOKABLE;

    Code:
    //myClass.h
    class CWinManager : public QObject
    {
        Q_OBJECT
    public:
        explicit CWinManager(QDeclarativeView *viewer,QDeclarativeItem *parent = 0);
        Q_INVOKABLE  void pushWindow(const QString &winFileName);
    signals:
    public slots:
    private:
        //member variables
        QDeclarativeView *m_pWinViewer;
    
    };
    Here is the my class's implemention:
    Code:
    //myClass.cpp
    CWinManager::CWinManager(QDeclarativeView *viewer,QDeclarativeItem *parent) :
        QObject(parent)
    {
        m_pWinViewer = viewer;
        if (NULL != m_pWinViewer)
            m_pWinViewer->rootContext()->setContextProperty("CWinManager",this);
    }
    
    void CWinManager::pushWindow(const QString &winFileName) {
        m_WinStack.append(winFileName);
        //Todo:when the window changed,peserve its status info.
        QString qml = winFileName;
        if (NULL != m_pWinViewer) {
    
            QDeclarativeContext *context = m_pWinViewer->rootContext();
            context->setContextProperty("CWinManager",this);
            m_pWinViewer->setSource(winFileName);
            m_pWinViewer->repaint();
        }
    }
    3. Define the second.qml
    Code:
    import QtQuick 1.0
    Rectangle {
        id: screen
        width: 1100; height: 520
        Text{
             text : "Second.qml"
        }
    }
    4.In the main.cpp, connect the C++ function modified by Q_INVOKABLE, here is the code:
    Code:
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QmlApplicationViewer viewer;
        CWinManager winManager(&viewer);
        viewer.show();
        viewer.setSource(QString("main.qml"));
        return app.exec();
    }
    The result:
    When I run the project,the main.qml can be displayed correctly,while the second can also be loaded,but when the setSouce in the CWinManager:ushWindow is called,there will occur a error and the applicatin will crash!
    Do u met the same problem ever? Can u do a favour to give me an instruction or a better solutions for this demand?

    Thks!

  2. #2
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,801

    Re: How to load the qml file in C++ dynamically?

    How do expect your CWinManager class be accessible in your main.qml file?
    To expose your class object to qml, you would need to set the object as the context property (see setContextProperty() ) of the view. I see you haven't done so.

    Refer to the Qt documentation: http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html
    Pankaj Nathani
    www.croozeus.com

Similar Threads

  1. Load class dynamically in J2ME
    By kurteknikk in forum Mobile Java General
    Replies: 14
    Last Post: 2010-01-04, 14:49
  2. Replies: 6
    Last Post: 2009-07-13, 17:26
  3. dynamically creating file..
    By kumar_a in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 2008-10-23, 08:22
  4. Replies: 3
    Last Post: 2008-01-29, 17:54
  5. [help !!!!] how to add a gif file to listbox dynamically ?
    By symbianyucca in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2008-01-28, 03:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×