// To build: link with OpenGL32.lib;qtmaind.lib;Qt5Widgetsd.lib;Qt5OpenGLd.lib;Qt5Cored.lib;Qt5Guid.lib #include #include #include #include #include #include // QMouseEvent position is correct with this widget class TestWidget : public QGLWidget { public: TestWidget(QWidget* parent) {} void mousePressEvent(QMouseEvent* e) { std::cout << "TestGLWidget::mousePressEvent():" << std::endl; std::cout << "e->pos(): " << e->pos().x() << ", " << e->pos().y() << std::endl; std::cout << "e->localPos(): " << e->localPos().x() << ", " << e->localPos().y() << std::endl; } }; // QMouseEvent position is *incorrect* with this widget, when the window is created on the second monitor. class TestGLWidget : public QGLWidget { public: TestGLWidget(QWidget* parent) : QGLWidget(parent) {} void mousePressEvent(QMouseEvent* e) { std::cout << "TestGLWidget::mousePressEvent():" << std::endl; std::cout << "e->pos(): " << e->pos().x() << ", " << e->pos().y() << std::endl; std::cout << "e->localPos(): " << e->localPos().x() << ", " << e->localPos().y() << std::endl; } void paintGL() { glClearColor(0, 0, 1, 0); // Clear with a blue colour. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw a red quad that should cover the entire viewport glColor3f(1, 0, 0); glBegin(GL_QUADS); glVertex2d(-1, -1); glVertex2d(1, -1); glVertex2d(1, 1); glVertex2d(-1, 1); glEnd(); } void resizeGL(int xres, int yres) { std::cout << "resizeGL(): xres: " << xres << ", yres: " << yres << std::endl; glViewport(0, 0, xres, yres); // NOTE: This results in a misplaced quad } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow main_window; main_window.setGeometry(/*x=*/3500, /*y=*/500, 500, 300); // NOTE: to replicate issue, choose x and y such that the window is positioned on second monitor. // Make central widget for main window QWidget* central_widget = new QWidget(&main_window); central_widget->setLayout(new QVBoxLayout(central_widget)); central_widget->layout()->setContentsMargins(0, 0, 0, 0); main_window.setCentralWidget(central_widget); // TestWidget* test_widget = new TestWidget(central_widget); // central_widget->layout()->addWidget(test_widget); TestGLWidget* test_glwidget = new TestGLWidget(central_widget); central_widget->layout()->addWidget(test_glwidget); main_window.show(); return app.exec(); }