QScreen manual test: add MouseMonitor to test multi-screen scenarios
- 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ł <spaz16@wp.pl> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
parent
4ac0686a6b
commit
3c08035b7b
@ -45,6 +45,61 @@
|
||||
#include <QStatusBar>
|
||||
#include <QLineEdit>
|
||||
#include <QDesktopWidget>
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
#include <QMouseEvent>
|
||||
|
||||
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user