How to display a QDeclarativeItem of my own type



  • Hi,
    I'm pretty new with QML and I don't really get everything. I have been looking the HappyBirthday example from Qt's web site and I think I understand it. So, I'm able to create a qml file, a class (called Node that inherits from QDeclarative item) and instance the class through qml with QDeclarativeEngine and a cast.
    My problem is that now I want to draw my object. I want it to be a circle with a label in the middle and I don't know how to do it because in mi qml file I have declared a Node, not a Rectangle, thus I cannot draw it...

    Let me show you my code by the moment:

    main.cpp
    @#include <QCoreApplication>
    #include <QDebug>
    #include <QObject>
    #include <QDeclarativeEngine>
    #include <QDeclarativeComponent>
    #include <QDeclarativeItem>
    #include <QString>
    #include <QTextStream>
    #include <QIODevice>

    #include "Node.h"

    int main(int argc, char* argv[]){

    QCoreApplication app(argc, argv);
    qmlRegisterType<Node>("CK", 1,0, "Node");
    QTextStream ts(stderr,QIODevice::WriteOnly);

    QDeclarativeEngine engine;
    QDeclarativeComponent component(&engine, QUrl("example.qml"));
    Node *node = qobject_cast<Node *>(component.create());
    qWarning() << component.errors();
    ts << node->label() <<endl;
    Node node2;
    ts << node2.label() <<endl;

    node->show();

    return 0;
    }@

    Node.h
    @#include <QObject>
    #include <QDeclarativeEngine>
    #include <QDeclarativeComponent>
    #include <QDeclarativeItem>
    #include <QString>

    class Node : public QDeclarativeItem{

    Q_OBJECT
    Q_PROPERTY(QString label READ label WRITE setLabel)

    private:
    QString _label;
    public:
    Node();
    public slots:
    QString label();
    void setLabel(QString lbl);

    };@

    and Node.cpp
    @#include "Node.h"

    Node::Node():QDeclarativeItem(NULL),_label("cpp") {}

    QString Node::label() {return _label;}
    void Node::setLabel(QString lbl) {_label = lbl;}@
    example.qml
    @import CK 1.0

    Node {
    id: theNode
    label: "from QML"
    }@

    What it does by now is just showing me that the instance made from qml and the one made from cpp as usual are diferent (teh label is not the same). This means that works.
    What I want now is to understant how I can make it visual.

    Thank you!



  • If you want to implement the rendering code in C++, you will need to reset the flag QGraphicsItem::ItemHasNoContents in —Āonstructor:

    @
    Node::Node():QDeclarativeItem(NULL),_label("cpp")
    {
    QGraphicsItem::setFlag(QGraphicsItem::ItemHasNoContents, false);
    }
    @

    And then implement a method of paint:

    @
    void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    //painter->draw...
    }
    @

    I hope that my short answer will help you.



  • Thank you for the answer!
    I will try it as soon as posible (this afternoon I hope).
    But, I thought that, since I inherit from QML, there would be a easy way to do it using the QtQuick stuff on the QML code. Is using a QPainter the only way to do it?

    Thank you again!

    EDIT:
    Thanks to your answer I just found this piece of doc: http://doc.qt.digia.com/qt/declarative-tutorials-extending-chapter1-basics.html . It seems that it is the right way to do it! Thank you again!


Log in to reply
 

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