Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get real top left corner of QToolButton



  • Hi,

    I have a QToolButton placed on a screen with features multiple frames and other layout widgets and I would like to extend other widgets on click of the button but for positioning of that custom widget, I need to find the top left corner of the QToolButton, I've tried btn.x() and btn.y() but the coordinates returned do clearly not align with the actual position of the button. I wrote a little test program which does not feature any layouts and btn.x() and btn.y() seem to be returning the correct values. How do I get the real position if the QToolButton is positioned inside layouts?



  • @cerr

    If your QToolButton is located in your widget (the one you want to map to), you can use mapToParent or mapTo(widget).
    If it's not a direct parent, you could mapToGlobal first and then mapFromGlobal to get the point in your widget's coordinates.


  • Lifetime Qt Champion

    Hi,

    QWidget::x value is relative to its parent, did you take that into account ?



  • @SGaist said in Get real top left corner of QToolButton:

    Hi,

    QWidget::x value is relative to its parent, did you take that into account ?

    OIps, you're right: "This property holds the x coordinate of the widget relative to its parent including any window frame"
    I want to get the absolute display position and should be looking at QRect then I guess... Thanks!


  • Lifetime Qt Champion

    @cerr
    Hi
    You should have a look at
    https://doc.qt.io/qt-5/qwidget.html#mapToGlobal

    if you by "absolute display position" mean the x,y on the screen.



  • @mrjj

    if you by "absolute display position" mean the x,y on the screen.

    I see, I wasn't particular enough, I actually mean the position in the Window. I will look at the link, Thank you!



  • @cerr

    If your QToolButton is located in your widget (the one you want to map to), you can use mapToParent or mapTo(widget).
    If it's not a direct parent, you could mapToGlobal first and then mapFromGlobal to get the point in your widget's coordinates.


Log in to reply