0

The title says it pretty much all:

I have two screens, and each time I create a QDialog it appears in the same screen as its parent. How can I make it appear in a different screen? Or should I use a different type of top-level widget?

The code I use to create the dialog is:

QDialog my_dialog = new QDialog(this,
                                Qt::WindowMaximizeButtonHint |
                                Qt::WindowCloseButtonHint);

...

EDIT: I have also tried using the QDesktopWidget which gives me a QScreen object that refers to the second screen. But then I don't find how to instruct the QDialog to use that QScreen (setting it as the parent doesn't work).

Daniel
  • 21,933
  • 14
  • 72
  • 101

2 Answers2

4

It is bad, that you edit your question without reading comments :(

// Your screen geometry:
QRect buildScreenGeometry()
{
  auto desktop = QApplication::desktop();
  QRect virtualRect;

  const auto n = desktop->screenCount();
  for ( auto i = 0; i < n; i++ )
    virtualRect |= desktop->screenGeometry(i);

  return virtualRect;
}

// Moving
auto dlg = new QDialog( someParent );
auto newPoint = QPoint( 2000, 0 ); // point on another screen
auto realPos = someParent->mapFromGlobal( newPoint );
dlg->move( realPos );

That's all.

UPDATE:

You should understand, that there are only ONE screen area with COMMON coordinate system, that contains ALL screens.

For example, you have 2 monitors with 800x600 resolution. First (main) monitor is standing left, and second standing right. In this case, coordinate system, that is available for your application is 1600x600. So, if your widget has 100x100 top-left position, on a first monitor, and you want to move it to another, you should call move(900x100); // 900 == screen1.width() + dialog.pos().x(). Then your widget will have 100x100 position on second monitor.

You should read Qt documentation.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • Thanks for the answer. This is the right one. Even though the first line doesn't make sense, I edited the question 16 hours ago, and no comment is older than that. – Daniel Mar 20 '15 at 14:47
0

You can use move on your QDialog, but be aware that move will set the QDialog position relative to it's parent.

You can get the Main Window's screen position and use that to setup the QDialog position. Just know that you're not guaranteed to have 2 screens on the end user machine.

For more information on move see: http://doc.qt.io/qt-5/application-windows.html#window-geometry

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • The `move()` function allows positioning only according to its parent's position. I cannot use that to move the `QDialog` to another screen, that is the main problem. – Daniel Mar 19 '15 at 22:13
  • 2
    @Mondkin you wrong. You can use move. Just call `mapFromGlobal` to calculate necessary coordinates. Or you may use `QWindow::setScreen`. – Dmitry Sazonov Mar 20 '15 at 08:09
  • @Mondkin Yup [SaZ](http://stackoverflow.com/users/1035613/saz) has the right of it. His answer is an implementation of moving which is fully workable. There are other methods as well, but they will all come down to using `move`. – Jonathan Mee Mar 20 '15 at 11:17
  • QDialog should not be positioned in parent's coordinates, but always in global coordinates instead. This is stated in the Qt doc (see "QDialog Class" description). So, its `move` never takes into account the parent's position! – RedSoft Jan 08 '23 at 15:40