双屏幕显示
界面程序,需要启动2个实例,分别放在两个屏幕上:
实现方式:
QDesktopWidget *desktop = QApplication::desktop();
int screenNum = desktop->numScreens();
1
2
获取当前环境的屏幕个数,如果为2个屏幕,返回值为2。
根据屏幕索引号获取屏幕位置,如果为2个屏幕,默认计算机主屏幕index=0,外接显示器index = 1:
int index=1;
QRect rect = desktop->screenGeometry(index);
1
2
在显示窗口前,调用setGeometry。
MainWindow* window = new MainWindow (NUll);
window ->setGeometry(rect);
window ->show();
另外一个实例采用相同的方式设定显示的屏幕。
注:
之前尝试使用QWidget *screen(int screen = -1);直接获取当前屏幕对于的QWidget 对象,发现不能生效。
QDesktopWidget *desktop = QApplication::desktop();
QWidget *parent = desktop -> screen(1);
MainWindow* window = new MainWindow (parent);
最后发现,在使用Qt-4.8版本中,windows下该接口没有实际功能。
/*qdesktopwidget_win.cpp*/
QWidget *QDesktopWidget::screen(int /* screen */)
{
// It seems that a Qt::WType_Desktop cannot be moved?
return this;
}
双屏幕间拖动
在主窗口类下,
mousePressEvent中,保存窗口原来的窗口索引old_index ;
mouseReleaseEvent获取当前的索引,如果与old_index不同,发送信号,通知切换屏幕;
切换屏幕槽函数:
void MainWindow::slotSwitchScreen(const int nScreenNo)
{
QRect oRect = QApplication::desktop()->screenGeometry(nScreenNo);
this->move(oRect.topLeft());
this->showFullScreen();
}
原文:https://blog.csdn.net/weixin_39763552/article/details/80422990