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. [solved] c++ equvalent of "property alias"

[solved] c++ equvalent of "property alias"

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

    Hi,

    What would be the c++ equivalent of "property alias" from qml?

    I have a class extending QQuickText in which I create 2 child QQuickText objects like this:
    @
    class XLabel : public QQuickText
    {
    Q_OBJECT

    public:
    QQuickText *probe, *label1, *label2;
    void createLabels();
    ....
    }

    void XLabel::createLabels(){
    label1 = new QQuickText(this);
    label2 = new QQuickText(this);
    // here the equivalent of property alias ...
    }
    @

    I need to expose these 2 child labels in the main object so they are accessible from QML.
    In QML I would do like this:

    @
    Text{

    property alias t1:label1; // <-- how can I do this in c++ ?
    property alias t2:label2;  
    
    Text{
        id:label1;
    }
    
    Text{
        id:label2;
    }
    

    }
    @

    So how can I do this from c++ ?

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

      I think there is no such mechanism in C++. You need to declare those properties by hand.

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Katlin
        wrote on last edited by
        #3

        You mean like we do with all the properties, using Q_PROPERTY ? Would that work on a QQuickText* variable ?

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

          [quote author="Katlin" date="1416997981"]You mean like we do with all the properties, using Q_PROPERTY ? Would that work on a QQuickText* variable ?[/quote]

          Yes and yes. QQuickText is still a Q_OBJECT, so all the magic from MOC is available here.

          (Z(:^

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Katlin
            wrote on last edited by
            #5

            I did that. I see that in QML it is not available. If I console.log it, i get undefined.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Katlin
              wrote on last edited by
              #6

              Correction: it works, it was just a typo.
              So now, I can address it just as if it would be declared in QML ?

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

                Yes, it should work. Remember the QML engine relies on NOTIFY signal to manage bindings, so you need to have that declared and used in your property functions.

                (Z(:^

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Katlin
                  wrote on last edited by
                  #8

                  Ok, it solved my problem, thx! Now on click I do have access to the sub-labels.
                  But now there is another problem; may I direct your attention to it: https://qt-project.org/forums/viewthread/50047/ ?

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

                    Thanks for the link, but this time I do not know the answer.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Katlin
                      wrote on last edited by
                      #10

                      Oki, again, thank you!

                      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