3

I m using Qt5 on linux, I want to display window form fullscreen on second screen (dual monitor)? I tried this code but it doesnt work. Is there any other way?

QRect screenres = QApplication::desktop()->screenGeometry(1/*screenNumber*/);
Widget *secondDisplay = new Widget(); // Use your QWidget
secondDisplay->move(QPoint(screenres.x(), screenres.y()));
secondDisplay->resize(screenres.width(), screenres.height());
Gates
  • 67
  • 3
  • 11
  • The problem is that you execute `secondDisplay->showFullScreen();` and this messes everything you did in the previous 2 lines. What happens if you comment the last line? – Iuliu Nov 07 '14 at 14:33
  • Tried! still not working. Getting several errors. Is there any alternative way. – Gates Nov 07 '14 at 14:43
  • What errors? In what way is it not working? What happens? Details would be helpful. – Iuliu Nov 07 '14 at 14:45
  • error: base operand of '->' has non-pointer type 'Widget' w->move(QPoint(screenres.x(), screenres.y()));// using my widget object here ^ – Gates Nov 07 '14 at 14:52
  • 1
    Well that's a compiler error...I thought that you had a typo...you forgot the `*` operator. Replace `SecondDisplay secondDisplay = new SecondDisplay();` with `SecondDisplay *secondDisplay = new SecondDisplay();` and come back with the results. – Iuliu Nov 07 '14 at 14:54
  • This is working but form isnt resizing to full screen and it is displaying on same monitor. I'll paste my code in question please check. – Gates Nov 07 '14 at 15:08
  • 1
    In your example you are showing `w` but resizing `secondDisplay` if you call `setWindowFlags(Qt::Window | Qt::FramelessWindowHint)` and `show()` on `secondDisplay` everything works fine. I tested on Windows, hope it does on GNU/Linux too. – Iuliu Nov 07 '14 at 15:19
  • Did it work? A feedback would be helpful. – Iuliu Nov 07 '14 at 15:53

2 Answers2

4

You can use QScreen.

QScreen *screen = QGuiApplication::screens()[1]; // specify which screen to use

SecondDisplay secondDisplay = new SecondDisplay(); // your widget

secondDisplay->move(screen->geometry().x(), screen->geometry().y());
secondDisplay->resize(screen->geometry().width(), screen->geometry().height());
secondDisplay->showFullScreen();
ahaltindis
  • 338
  • 1
  • 6
2

One way of doing it in Qt5 is to use QWindow::setScreen to set the screen on which the window should be shown. QWidget has a windowHandle() that returns the pointer to the QWindow.

Here is how to show your widget in the last screen in full-screen mode :

secondDisplay->show();
secondDisplay->windowHandle()->setScreen(qApp->screens().last());
secondDisplay->showFullScreen();
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Your answer helped me but I have additional question. Why must I do show() before doing windowHandle().setScreen()? Is it possible to get the QWindow object before showing it? Note: I am using Python and not C++, so your response in Python would be helpful. – Zythyr Aug 12 '19 at 04:59
  • @Zythyr This is the way it works, may be it's a bug. As long as I remember setting window screen before it 's shown does not work always as expected. I have not tried with new versions of Qt. May be it's fixed now. – Nejat Aug 12 '19 at 19:26
  • show() is what actually creates the window, so there is no window handle before that happens – BrandonL Jul 26 '22 at 16:11