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 reset anchors?

How to reset anchors?

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 8.9k 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.
  • F Offline
    F Offline
    franziss
    wrote on last edited by
    #1

    I read in http://doc.qt.nokia.com/4.7-snapshot/qml-mousearea.html#drag.active-prop that in order to drag an object which is anchored to its parent, we can undefined the anchors in the onPressed signal. I tried it but I have the compile error

    @
    Expected token `;'
    anchors.bottom: undefined
    @

    Below is my code, icon is under a rectangle
    @
    Image {
    id: icon
    width: 64
    height: 64
    source: "icon.gif"
    anchors.bottom: parent.bottom

           MouseArea {
               id: iconMouseArea
               anchors.fill: parent
               width: 64
               height: 64
    
               drag.target: parent
               drag.axis: Drag.XandYAxis
    
               onPressed: {
                   anchors.bottom: undefined
               }
    
           }
       }
    

    @

    Can someone help me out? Thanks!

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      I think you need to explicitly state which item you are changing anchors on with something like this in a state.

      @
      AnchorChanges {
      target: icon
      anchors.bottom: undefined
      }
      @

      You onPressed: handler can then simply change to that state. Does that work for you?

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franziss
        wrote on last edited by
        #3

        Thank you very much for your help ZapB, I implemented a state and use AnchorChanges, it works now.

        Is it possible to do anchor changes in c++, instead of qml?

        H 1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          Hmmm, good question. I'm not sure. It's not something I have ever tried. In theory I would think yes as the anchors are just properties like everything else. Have a play around with it and see if it is possible. I would be interested to hear the outcome of your experiments.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • F Offline
            F Offline
            franziss
            wrote on last edited by
            #5

            I tried the following slot, which is called when I move an object (e.g. icon) into a slide (e.g. rectangle). Once the object is in the slide, I want to set the object as a child of the slide, so that when I drag the slide, the object will move with it.

            In this function, both slide and object are cast as QGraphicsWidgets and then I try to use QGraphicsAnchorLayout to anchor the object inside slide, but it does not work.

            @
            void cc::putObjectIntoSlide(QGraphicsObject * slide, QGraphicsObject * object)
            {
            if (object != NULL)
            {
            qDebug("got object in slide");
            object->setParent(slide);
            QGraphicsWidget *a = new QGraphicsWidget(slide);
            QGraphicsWidget *b = new QGraphicsWidget(object);
            QGraphicsAnchorLayout *la = new QGraphicsAnchorLayout;
            QGraphicsAnchorLayout *lb = new QGraphicsAnchorLayout;
            a->setLayout(la);
            b->setLayout(lb);
            la->addAnchor(la, Qt::AnchorBottom, lb, Qt::AnchorBottom);
            }
            else
            {
            qDebug("no object");
            }
            }
            @

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              QGraphicsAnchorLayout has nothing to do with anchors in QML. I'm not sure if the QML anchors are exported in any useable way in C++ other than perhaps by way of the setProperty() and property() functions of QObject.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • F Offline
                F Offline
                franziss
                wrote on last edited by
                #7

                Ok, I tried using setProperty as follows, but it doesn't work

                @
                object->setProperty("anchors.bottom","parent.bottom");
                @

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

                  I'd think that anchors is actually an object (or some other collection of properties) of its own, so I am not surprised that that doesn't work. No idea how it would work though.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    franziss
                    wrote on last edited by
                    #9

                    If I drag an object A into object B, and make object A a child of object B, how do I anchor A to B?

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Hit Tiger Tonight
                      wrote on last edited by
                      #10

                      @franziss said in How to reset anchors?:

                      Thank you very much for your help ZapB, I implemented a state and use AnchorChanges, it works now.

                      Is it possible to do anchor changes in c++, instead of qml?

                      Yes, you can. Please refer to: https://forum.qt.io/topic/143222/how-to-pass-an-object-ref-from-c-to-qml

                      1 Reply Last reply
                      0
                      • F franziss

                        Thank you very much for your help ZapB, I implemented a state and use AnchorChanges, it works now.

                        Is it possible to do anchor changes in c++, instead of qml?

                        H Offline
                        H Offline
                        Hit Tiger Tonight
                        wrote on last edited by
                        #11

                        @franziss said in How to reset anchors?:

                        Thank you very much for your help ZapB, I implemented a state and use AnchorChanges, it works now.

                        Is it possible to do anchor changes in c++, instead of qml?

                        Yes, you can. Please refer to: https://forum.qt.io/topic/143222/how-to-pass-an-object-ref-from-c-to-qml

                        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