Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to display a QDeclarativeItem of my own type
Forum Updated to NodeBB v4.3 + New Features

How to display a QDeclarativeItem of my own type

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    excalibur1491
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • podsvirovP Offline
      podsvirovP Offline
      podsvirov
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        excalibur1491
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved