Independant QML class



  • Hello everyone,

    I am looking for a way to make an independant C++ class that can launch a QML view when needed. The code below seems to open the window but closes it. Can someone help me please ?

    main.cpp
    @
    #include <QApplication>
    #include "Widget.h"

    int main (int argc, char **argv)
    {
    QApplication app(argc, argv);
    Widget *widget = new Widget;
    return app.exec();
    }
    @

    Widget.h
    @
    #ifndef WIDGET_H
    #define WIDGET_H

    #include <iostream>
    #include <QWidget>

    using namespace std;

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    Widget(QWidget *parent = 0);

    public slots:
    void testClicked (void);
    };

    #endif // WIDGET_H
    @

    Widget.cpp
    @
    #include "Widget.h"
    #include <QtGui>
    #include <QtDeclarative>

    Widget::Widget(QWidget *parent):QWidget (parent)
    {
    // Here is the big deal !
    QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile("main.qml"));
    QObject *item = view.rootObject();
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    QObject::connect(item, SIGNAL(qmlTestClick()), this, SLOT(testClicked ()));
    view.setMinimumSize(500,500);
    view.show();
    }

    void Widget::testClicked (void)
    {
    cout<<"Signal recu du bouton QML"<<endl;
    }
    @

    main.qml
    @

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle
    {

    signal qmlTestClick()
    
    id: principal
    width: 500
    height: 500
    
    Rectangle
    {
    
        id: rectangle1
        color: "lightgrey"
        anchors.top: parent.top
        anchors.topMargin: 15
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 65
        anchors.right: parent.right
        anchors.rightMargin: 15
        anchors.left: parent.left
        anchors.leftMargin: 14
    }
    Rectangle
    {
        id: bouton
        color: "grey"
        radius: 3
        anchors.bottom: parent.bottom
    
        anchors.bottomMargin: 14
        anchors.top: rectangle1.bottom
        anchors.topMargin: 15
        anchors.right: parent.right
        anchors.rightMargin: 15
        anchors.left: parent.left
        anchors.leftMargin: 14
    
        Text
        {
            id: nomBouton
            color: "white"
            anchors.centerIn: parent
            anchors.horizontalCenter: parent.horizontalCenter
            text: "PushButton"
        }
    
        Behavior on color { PropertyAnimation { from: "blue"; to: "grey"; duration: 500 } }
    
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                bouton.color = "blue"
                qmlTestClick()  // envoi du signal
            }
        }
    }
    

    }
    @



  • You mus create the the QDeclarativeView on the heap (using new). It's also a good idea to either give it a parent, and/or add a QDeclarativeView* view; member to your class



  • Oh thanks ! I all try it.



  • Well thank you very much !
    I understand more about the consequences not putting it on the heap. Not using new is like not using a malloc in C... tell me if I'm wrong. Is it referring to lvalue too ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.