Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mapping widget positions to other widgets...
Forum Updated to NodeBB v4.3 + New Features

Mapping widget positions to other widgets...

Scheduled Pinned Locked Moved Solved General and Desktop
coordinate
7 Posts 3 Posters 2.1k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    How can I map my widgets positions between each other...

    Here is video of it failing, I want the "move Me" button to move to the same height as tree view.

    2022_05_25---09-25-13_capture_3929.gif

    Here is python example:

    from functools import partial
    
    from PySide6.QtCore import *
    from PySide6.QtGui import *
    from PySide6.QtWidgets import *
    
    import sys
    
    a = QApplication()
    
    w = QWidget()
    layw = QGridLayout(w)
    spliter = QSplitter()
    layw.addWidget(spliter)
    wa = QWidget()
    la = QGridLayout(wa)
    btn = QPushButton("Calculate post")
    btn2 = QPushButton("meh");
    lab = QLabel("alala")
    view = QTreeView()
    
    la.addWidget(btn)
    la.addWidget(btn2)
    la.addWidget(lab)
    splitterOther = QSplitter()
    splitterOther.setOrientation(Qt.Vertical)
    splitterOther.addWidget(QLabel("mdmemem"))
    splitterOther.addWidget(view)
    la.addWidget(splitterOther)
    
    other = QWidget()
    moveMe = QPushButton("move Me", other)
    
    
    spliter.addWidget(wa)
    spliter.addWidget(other)
    
    def moveButton(widget, view, targetWidget,targetButton):
        pos = view.mapToParent(view.viewport().geometry().topLeft())
        loc = QPoint(0,pos.y())
        targetButton.move(loc)
    
    btn.released.connect(partial(moveButton,wa,view,other,moveMe))
    w.show()
    sys.exit(a.exec())
    
    

    Do I need to recursively traverse all the way to top or something? Is there a way to "jump" ahead somehow?

    jsulmJ M 2 Replies Last reply
    0
    • D Dariusz

      @jsulm nope, as you can see in example, the tree view is child of few other widgets comparing to moveMe widget.
      @mpergand hmm but its an nested widget... so I dont think this will work. But still awesome find thanks!

      I take I have to manuall traverse each level to find correct position in relation to other widget. Bummer there is no "global" widget->mapToTargetWidget(targetWidget,pos) functions that would do it for me :c

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @Dariusz You could transfer first coordinates from the tree view to global coordinates and then to local coordinates of the widget where the button is https://doc.qt.io/qt-5/qwidget.html#mapToGlobal and https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      3
      • D Dariusz

        Hey

        How can I map my widgets positions between each other...

        Here is video of it failing, I want the "move Me" button to move to the same height as tree view.

        2022_05_25---09-25-13_capture_3929.gif

        Here is python example:

        from functools import partial
        
        from PySide6.QtCore import *
        from PySide6.QtGui import *
        from PySide6.QtWidgets import *
        
        import sys
        
        a = QApplication()
        
        w = QWidget()
        layw = QGridLayout(w)
        spliter = QSplitter()
        layw.addWidget(spliter)
        wa = QWidget()
        la = QGridLayout(wa)
        btn = QPushButton("Calculate post")
        btn2 = QPushButton("meh");
        lab = QLabel("alala")
        view = QTreeView()
        
        la.addWidget(btn)
        la.addWidget(btn2)
        la.addWidget(lab)
        splitterOther = QSplitter()
        splitterOther.setOrientation(Qt.Vertical)
        splitterOther.addWidget(QLabel("mdmemem"))
        splitterOther.addWidget(view)
        la.addWidget(splitterOther)
        
        other = QWidget()
        moveMe = QPushButton("move Me", other)
        
        
        spliter.addWidget(wa)
        spliter.addWidget(other)
        
        def moveButton(widget, view, targetWidget,targetButton):
            pos = view.mapToParent(view.viewport().geometry().topLeft())
            loc = QPoint(0,pos.y())
            targetButton.move(loc)
        
        btn.released.connect(partial(moveButton,wa,view,other,moveMe))
        w.show()
        sys.exit(a.exec())
        
        

        Do I need to recursively traverse all the way to top or something? Is there a way to "jump" ahead somehow?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Dariusz said in Mapping widget positions to other widgets...:

        I want the "move Me" button to move to the same height as tree view

        If both have same parent widget then simply use y coordinate from the tree view for the button

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Dariusz

          Hey

          How can I map my widgets positions between each other...

          Here is video of it failing, I want the "move Me" button to move to the same height as tree view.

          2022_05_25---09-25-13_capture_3929.gif

          Here is python example:

          from functools import partial
          
          from PySide6.QtCore import *
          from PySide6.QtGui import *
          from PySide6.QtWidgets import *
          
          import sys
          
          a = QApplication()
          
          w = QWidget()
          layw = QGridLayout(w)
          spliter = QSplitter()
          layw.addWidget(spliter)
          wa = QWidget()
          la = QGridLayout(wa)
          btn = QPushButton("Calculate post")
          btn2 = QPushButton("meh");
          lab = QLabel("alala")
          view = QTreeView()
          
          la.addWidget(btn)
          la.addWidget(btn2)
          la.addWidget(lab)
          splitterOther = QSplitter()
          splitterOther.setOrientation(Qt.Vertical)
          splitterOther.addWidget(QLabel("mdmemem"))
          splitterOther.addWidget(view)
          la.addWidget(splitterOther)
          
          other = QWidget()
          moveMe = QPushButton("move Me", other)
          
          
          spliter.addWidget(wa)
          spliter.addWidget(other)
          
          def moveButton(widget, view, targetWidget,targetButton):
              pos = view.mapToParent(view.viewport().geometry().topLeft())
              loc = QPoint(0,pos.y())
              targetButton.move(loc)
          
          btn.released.connect(partial(moveButton,wa,view,other,moveMe))
          w.show()
          sys.exit(a.exec())
          
          

          Do I need to recursively traverse all the way to top or something? Is there a way to "jump" ahead somehow?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #3

          @Dariusz
          You can retreive the geometry of any widget in a gridlayout with:
          QRect QGridLayout::cellRect(int row, int column) const

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #4

            @jsulm nope, as you can see in example, the tree view is child of few other widgets comparing to moveMe widget.
            @mpergand hmm but its an nested widget... so I dont think this will work. But still awesome find thanks!

            I take I have to manuall traverse each level to find correct position in relation to other widget. Bummer there is no "global" widget->mapToTargetWidget(targetWidget,pos) functions that would do it for me :c

            jsulmJ M 2 Replies Last reply
            0
            • D Dariusz

              @jsulm nope, as you can see in example, the tree view is child of few other widgets comparing to moveMe widget.
              @mpergand hmm but its an nested widget... so I dont think this will work. But still awesome find thanks!

              I take I have to manuall traverse each level to find correct position in relation to other widget. Bummer there is no "global" widget->mapToTargetWidget(targetWidget,pos) functions that would do it for me :c

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Dariusz You could transfer first coordinates from the tree view to global coordinates and then to local coordinates of the widget where the button is https://doc.qt.io/qt-5/qwidget.html#mapToGlobal and https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply
              3
              • D Dariusz

                @jsulm nope, as you can see in example, the tree view is child of few other widgets comparing to moveMe widget.
                @mpergand hmm but its an nested widget... so I dont think this will work. But still awesome find thanks!

                I take I have to manuall traverse each level to find correct position in relation to other widget. Bummer there is no "global" widget->mapToTargetWidget(targetWidget,pos) functions that would do it for me :c

                M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #6

                @Dariusz said in Mapping widget positions to other widgets...:

                hmm but its an nested widget... so I dont think this will work. But still awesome find thanks!

                Why not, as @jsulm said, you have to make the right conversions between the views.

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Dariusz You could transfer first coordinates from the tree view to global coordinates and then to local coordinates of the widget where the button is https://doc.qt.io/qt-5/qwidget.html#mapToGlobal and https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

                  D Offline
                  D Offline
                  Dariusz
                  wrote on last edited by
                  #7

                  @jsulm Yep that works, awesome thanks!

                  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