Mapping widget positions to other widgets...
-
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.
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?
-
@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
-
@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
-
@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
-
@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
-
@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.