Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-screen setPositionRelativeToWidget not correct #22

Open
windtail opened this issue Nov 6, 2024 · 3 comments
Open

multi-screen setPositionRelativeToWidget not correct #22

windtail opened this issue Nov 6, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@windtail
Copy link

windtail commented Nov 6, 2024

Hi,

Thanks for your great job!

When __position_relative_to_widget is not on primaryScreen, the toast will be in wrong screen.

def __get_bounds(self) -> QRect:
        # ...
        if Toast.__position_relative_to_widget is not None:
            return Toast.__position_relative_to_widget.geometry()
        # ...

i think we need a global rect here, but according to qt doc

This property holds the geometry of the widget relative to its parent and excluding the window frame

so, maybe the code should be changed to

if Toast.__position_relative_to_widget is not None:
    rect = Toast.__position_relative_to_widget.geometry()
    top_left = Toast.__position_relative_to_widget.mapToGlobal(rect.topLeft())
    return QRect(top_left, rect.size())
@niklashenning
Copy link
Owner

niklashenning commented Nov 6, 2024

Hi, thanks for opening this issue.

I see your point, but I'm not able to reproduce this issue for some reason. When I call Toast.setPositionRelativeToWidget(widget) and then move the widget to another screen, the toasts do show on the correct screen. Could you please provide a minimal reproducible example so I can look into this further?

@windtail
Copy link
Author

Hi, thanks for quit reply.

Here is the minimal example.

import sys

from PySide6.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QLabel
from pyqttoast import Toast

class Win(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)

        layout = QVBoxLayout()

        self._label = QLabel("text", self)
        layout.addWidget(self._label, stretch=1)

        self._btn = QPushButton("click me", self)
        self._btn.clicked.connect(self._on_btn_clicked)
        layout.addWidget(self._btn)

        self.setLayout(layout)


    def _on_btn_clicked(self):
        toast = Toast()
        # FIXME: the problem is here, relative to a sub-widget
        toast.setPositionRelativeToWidget(self._label)
        toast.setDuration(5000)
        toast.setText("I am in primary screen")
        toast.show()


def main():
    app = QApplication(sys.argv)

    screens = app.screens()
    # we need two screens to reveal the bug
    assert len(screens) > 1
    screen = screens[1]

    win = Win()
    win.setScreen(screen)
    win.move(screen.geometry().topLeft())
    win.show()

    app.exec()

if __name__ == '__main__':
    main()

@niklashenning niklashenning added the bug Something isn't working label Nov 12, 2024
@niklashenning
Copy link
Owner

Thanks, now I understand. I was only thinking about top level widgets where mapping to global isn't necessary.
This will be fixed in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants