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. Does qmlregistertype, after usage in qml file, call the constructor of the c++ object? [SOLVED]
QtWS25 Last Chance

Does qmlregistertype, after usage in qml file, call the constructor of the c++ object? [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 2 Posters 4.6k Views
  • 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.
  • R Offline
    R Offline
    rocketchicken
    wrote on last edited by
    #1

    I was wondering about the behaviour of the registered type with the qml engine.
    I am working in cascades qt so qt 4.8 ish.

    So I registered a type, with qmlregistertype.
    Created the qml type in a qml document.

    When the Qml document is created there is no errors in the device log.
    But the constructor for object do not appeared to be called at all.

    Does the Qml instanced object have its c++ counterpart constructor called on creation , or od I have or invoke it?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to use the type in your QML for the constructor to be called, although from your description I take it that that is what you have done. How are you checking whether the constructor was called?

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rocketchicken
        wrote on last edited by
        #3

        I put a qDebug() << "The constructor was called"

        Then I put a qDebug << "This function was called"

        Okay debug output for device now works.

        I have an issue though, it appears that it is calling my default constructor what I need it to call though is the overridden constructor.

        The reason for this is, that I inherited the bb::cascades::multimedia::Camera.
        To create own qml camera.

        and I have a constructor like so

        myCamera::myCamera()
        {
        qDebug() << "HELLO I AM DONE DEFAULT ()!!!!";

        }

        myCamera::myCamera(bb::cascades::Container* parent):
        Camera(parent)
        {
        qDebug() << "HELLO I AM DONE OVERRIDEN()!!!!";

        }

        It ends up calling the first constructor, and then my signals and slots will not connect ,even though they inherit public signals from the bb10 camera I just inherited.

        I need a way to invoke the second constructor any clues?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Use C++11 if you dare. Older C++ standards do not allow calling constructors from within other constructors.

          Or "distribute" your code a bit. For example, you can add a Q_INVOKABLE init() method and call it from QML in Component.onCompleted() to set your object up.

          (Z(:^

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rocketchicken
            wrote on last edited by
            #5

            Yeah that does not work to well, I don't think its a good idea to inherit c++ cascades multimedia camera, object and expect it to reflect its self in qml ... is this correct?

            because from what I can tell the qdeclarative parser only allows the default constructor to be called.

            So this is what I have

            bb10 camera -> myCamera
            qmlregistertype<myCamera>

            connect signals and slots
            @
            connect(this,SIGNAL(this->touch(bb::cascades::TouchEvent*)),this,SLOT(OnTouch()));

            // Signaled when the rear of front cameras are opened.
            connect(this->parent(),SIGNAL(this->parent->cameraOpened()),this,SLOT(OnCameraOpened()));

            // Signaled when a picture is taken.
            connect(this,SIGNAL(this->shutterFired()),this,SLOT(OnShutterFired()));

            connect(this,SIGNAL(this->viewfinderStarted()),this,SLOT(OnViewFinderStarted()));

            // Signaled when a picture is about to be saved.
            connect(this,SIGNAL(this->photoSaved(const QString &, quint64)),this,SLOT(OnPhotoSaved()));
            qDebug() << "HELLO I AM DONE CONNECTING!!!!";
            @

            Then the debug output messages say that there are no corresponding signals.

            A better path of design I guess would to be compose the camera object in this class and wrap some of the functions of the camera to my own MyCamera class?

            I am just wondering what the best practice is, from all the code examples I have seen on the bb10 sdk site they are just horrible

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Please wrap that code in '@' tags, it's difficult to read.

              (Z(:^

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rocketchicken
                wrote on last edited by
                #7

                Sorry I forgot about that haha

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  That line looks wrong:
                  @
                  connect(this->parent(),SIGNAL(this->parent->cameraOpened()),this,SLOT(OnCameraOpened()));
                  @

                  Should be SIGNAL(cameraOpened()). Also, all remaining statements connect this and not parent, so I'm a bit confused.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rocketchicken
                    wrote on last edited by
                    #9

                    @
                    qDebug() << "HELLO I AM CONNECTING!!!!";
                    // These are private slots that is they are used internally for camera functionality.
                    // These functions should not really be exposed to Qml for use in the Qml interface.

                    // Connecting the inherited signal touch event to that of the non implicit onTouched.
                    // Since we do not have a capture camera button touching the surface will result in a
                    // capture

                    connect(this,SIGNAL(touch(bb::cascades::TouchEvent*)),this,SLOT(OnTouch()));

                    // Signaled when the rear of front cameras are opened.
                    connect(this,SIGNAL(cameraOpened()),this,SLOT(OnCameraOpened()));

                    // Signaled when a picture is taken.
                    connect(this,SIGNAL(shutterFired()),this,SLOT(OnShutterFired()));

                    connect(this,SIGNAL(viewfinderStarted()),this,SLOT(OnViewFinderStarted()));

                    // Signaled when a picture is about to be saved.
                    connect(this,SIGNAL(photoSaved(const QString &, quint64)),this,SLOT(OnPhotoSaved()));
                    qDebug() << "HELLO I AM DONE CONNECTING!!!!";
                    @

                    Okay I have fixed it to be this since I made a mistake.
                    I had assumed that if I passed this (Camera) I would have to refer to its function using the this pointer.

                    So that log error warning went away.

                    and Camera actually functions now.

                    Thanks I think this should be resolved now =)

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      I'm glad to hear that! Happy coding!

                      Please add "[Solved]" to thread's title (you need to edit your initial post) if you have a spare minute.

                      (Z(:^

                      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