From 3c08035b7b29e48e749a92cbdf05432a57f4108d Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 6 Jan 2016 14:47:00 +0100 Subject: QScreen manual test: add MouseMonitor to test multi-screen scenarios MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - QMouseEvent::screenPos() should be global desktop position - QDesktopWidget::screenNumber() should tell the correct screen - QGuiApplication::topLevelAt(screenPos) should find the window where the mouse is clicked Change-Id: I9a63ab3ee1944b7246551d0f3d5e37f0d2aa5457 Reviewed-by: Błażej Szczygieł Reviewed-by: Friedemann Kleint --- tests/manual/qscreen/main.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tests/manual/qscreen') diff --git a/tests/manual/qscreen/main.cpp b/tests/manual/qscreen/main.cpp index 298996a59b..13d4145a68 100644 --- a/tests/manual/qscreen/main.cpp +++ b/tests/manual/qscreen/main.cpp @@ -45,6 +45,61 @@ #include #include #include +#include +#include +#include + + +class MouseMonitor : public QLabel { + Q_OBJECT +public: + MouseMonitor() : m_grabbed(false) { + setMinimumSize(540, 240); + setAlignment(Qt::AlignCenter); + setMouseTracking(true); + setWindowTitle(QLatin1String("Mouse Monitor")); + updateText(); + } + + void updateText() { + QString txt = m_grabbed ? + QLatin1String("Left-click to test QGuiApplication::topLevelAt(click pos)\nRight-click to ungrab\n") : + QLatin1String("Left-click to grab mouse\n"); + if (!m_cursorPos.isNull()) { + txt += QString(QLatin1String("Current mouse position: %1, %2 on screen %3\n")) + .arg(m_cursorPos.x()).arg(m_cursorPos.y()).arg(QApplication::desktop()->screenNumber(m_cursorPos)); + if (QGuiApplication::mouseButtons() & Qt::LeftButton) { + QWindow *win = QGuiApplication::topLevelAt(m_cursorPos); + txt += QString(QLatin1String("Top-level window found? %1\n")) + .arg(win ? (win->title().isEmpty() ? "no title" : win->title()) : "none"); + } + } + setText(txt); + } + +protected: + void mouseMoveEvent(QMouseEvent *ev) Q_DECL_OVERRIDE { + m_cursorPos = ev->screenPos().toPoint(); + updateText(); + } + + void mousePressEvent(QMouseEvent *ev) Q_DECL_OVERRIDE { + m_cursorPos = ev->screenPos().toPoint(); + qDebug() << "top level @" << m_cursorPos << ":" << QGuiApplication::topLevelAt(m_cursorPos); + updateText(); + if (!m_grabbed) { + grabMouse(Qt::CrossCursor); + m_grabbed = true; + } else if (ev->button() == Qt::RightButton) { + setVisible(false); + deleteLater(); + } + } + +private: + QPoint m_cursorPos; + bool m_grabbed; +}; class ScreenPropertyWatcher : public PropertyWatcher { @@ -101,6 +156,7 @@ public: protected: bool event(QEvent *event) Q_DECL_OVERRIDE; + void startMouseMonitor(); private: const QString m_annotation; @@ -124,6 +180,11 @@ ScreenWatcherMainWindow::ScreenWatcherMainWindow(QScreen *screen) a = fileMenu->addAction(QLatin1String("Quit")); a->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); connect(a, SIGNAL(triggered()), qApp, SLOT(quit())); + + QMenu *toolsMenu = menuBar()->addMenu(QLatin1String("&Tools")); + a = toolsMenu->addAction(QLatin1String("Mouse Monitor")); + a->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M)); + connect(a, &QAction::triggered, this, &ScreenWatcherMainWindow::startMouseMonitor); } static inline QString msgScreenChange(const QWidget *w, const QScreen *oldScreen, const QScreen *newScreen) @@ -159,6 +220,12 @@ bool ScreenWatcherMainWindow::event(QEvent *event) return QMainWindow::event(event); } +void ScreenWatcherMainWindow::startMouseMonitor() +{ + MouseMonitor *mm = new MouseMonitor(); + mm->show(); +} + void screenAdded(QScreen* screen) { screen->setOrientationUpdateMask((Qt::ScreenOrientations)0x0F); -- cgit v1.2.3