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. C++ Qml ctor problem
Forum Updated to NodeBB v4.3 + New Features

C++ Qml ctor problem

Scheduled Pinned Locked Moved QML and Qt Quick
19 Posts 3 Posters 6.7k 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.
  • A Offline
    A Offline
    aabc
    wrote on last edited by
    #1

    I created a qml extension in c++ (inherits QObject) and when debugging it i discivered that some of the READ and WRITE functions of some properties are called before the ctor has finished.
    Is it possible or am I missing something ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Could you show us your code, please?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aabc
        wrote on last edited by
        #3

        @
        class MyItem: public QObject
        {
        Q_OBJECT

        Q_PROPERTY(int key READ key WRITE setKey NOTIFY keyChanged)
        

        public:
        explicit MyItem(QObject* _parent = 0);

        const int key() const;
        void setKey(const int key);
        @

        setKey is called in the middle of the ctor

        Edit: please use @ tags around code sections; Andre

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          OK, nothing weird in that code section... I assume you're not, directly or indirectly, calling it yourself?
          Otherwise, it would be time to break out the debugger, set a breakpoint, and see what happens with the actual use of your item.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aabc
            wrote on last edited by
            #5

            In the ctor i set the val of m_key to be 8 but when key() is called on the first time (because of qml object that reads it) it return 0

            When i use the debugger i can see that it goes to key() in the middle of the ctor code

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Can you trace how you get from the creator code to the key() method then?

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thisisbhaskar
                wrote on last edited by
                #7

                Was confused what this "ctor" is.... after some thinking and guessing.. it looks like its "constructor" :).

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thisisbhaskar
                  wrote on last edited by
                  #8

                  Its time you post some more code.. through out your class function definitions and your qml code where this class variables are referenced. You said you are setting value of m_key inside your constructor, are you using setKey() to do that or you are doing a direct assignment...

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aabc
                    wrote on last edited by
                    #9

                    I'm doing a direct assignment

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      thisisbhaskar
                      wrote on last edited by
                      #10

                      And how are you accessing this variables in qml side.. are you binding it to another variable and then reading that variable.. if this is the case, direct assignment would not help.

                      @
                      MyItem {
                      id: myItem
                      }

                      property int myVar : myItem.key
                      @

                      In the above code, myVar will not be updated to 8 when you directly assign 8 to m_key in C++ code. You will have to use setKey(8) for that to happen. Can you check this.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aabc
                        wrote on last edited by
                        #11

                        Where should i call the setKey() ? In the ctor?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          In your constructor, you should be safe, but you do not show us the constructor, so we can't be sure what happens there. Note that you might want to change this:

                          @
                          MyItem::MyItem(QObject* _parent):
                          QObject(_parent)
                          {
                          m_key = 8;
                          }
                          @

                          to

                          @
                          MyItem::MyItem(QObject* _parent):
                          QObject(_parent),
                          m_key(8)
                          {
                          }
                          @

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            thisisbhaskar
                            wrote on last edited by
                            #13

                            yes.. try that.

                            [quote author="Andre" date="1309169104"]Can you trace how you get from the creator code to the key() method then?[/quote]

                            Like Andre said, can you trace where are you getting this key() call from. Is it coming form qml variable binding??

                            Pasting some code always helps.. Can you paste your qml code in which you create Myitem class instance and how you are accessing key value ??

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              aabc
                              wrote on last edited by
                              #14

                              @MyItem::MyItem(QObject* _parent): QObject(_parent), m_key(8) { }
                              @

                              this is exactly what i'm doing.

                              key is a read only property and in my qml file the code is:

                              @MyItem
                              {
                              id: myItem
                              }

                              Text
                              {
                                  id: textElement
                                  anchors.fill: parent
                                  horizontalAlignment: Text.AlignHCenter
                                  verticalAlignment: Text.AlignVCenter
                                  text: " Hello "
                                  font.pointSize: myItem.key
                              }@
                              
                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                thisisbhaskar
                                wrote on last edited by
                                #15

                                Interesting problem.. let me make my hands dirty and try this out.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  Weird. I'd say it is impossible that your key is read before the constructor is finished then. Only a decent trace will show where the issue comes from, I think.

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    thisisbhaskar
                                    wrote on last edited by
                                    #17

                                    works like a charm for me.. copying my code.. can you please check where you are going wrong

                                    myitem.h
                                    @#ifndef MYITEM_H
                                    #define MYITEM_H
                                    #include <QObject>
                                    class MyItem : public QObject
                                    {
                                    Q_OBJECT
                                    Q_PROPERTY(int key READ key WRITE setKey NOTIFY keyChanged)
                                    public:
                                    explicit MyItem(QObject *parent = 0);
                                    const int key() const;
                                    void setKey(const int key);
                                    signals:
                                    void keyChanged();
                                    public slots:
                                    private:
                                    int m_key;

                                    };
                                    #endif // MYITEM_H@

                                    myitem.cpp
                                    @#include "myitem.h"
                                    MyItem::MyItem(QObject *parent) :
                                    QObject(parent),m_key(24)
                                    {
                                    }
                                    const int MyItem::key() const
                                    {
                                    return m_key;
                                    }
                                    void MyItem::setKey(const int key)
                                    {
                                    m_key = key;
                                    }@

                                    main.cpp
                                    @#include <QtGui/QApplication>
                                    #include "qmlapplicationviewer.h"
                                    #include <QDeclarativeContext>
                                    #include <QtDeclarative>
                                    #include "myitem.h"

                                    int main(int argc, char *argv[])
                                    {
                                    QApplication app(argc, argv);
                                    QDeclarativeView *viewer = new QDeclarativeView;
                                    qmlRegisterType<MyItem>("MyItemLibrary", 1, 0, "MyItem");
                                    viewer->setSource(QUrl::fromLocalFile("qml/Example/main.qml"));
                                    viewer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
                                    viewer->showFullScreen();
                                    return app.exec();
                                    }@

                                    main.qml
                                    @import QtQuick 1.0
                                    import MyItemLibrary 1.0
                                    Rectangle {
                                    id: mainItem
                                    MyItem
                                    {
                                    id: myItem
                                    }
                                    Text
                                    {
                                    id: textElement
                                    anchors.fill: parent
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    text: " Hello "
                                    font.pointSize: myItem.key
                                    }

                                    }@

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      aabc
                                      wrote on last edited by
                                      #18

                                      key should be a read only property without setkey()

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        thisisbhaskar
                                        wrote on last edited by
                                        #19

                                        is it working? where did you go wrong?

                                        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