use*_*240 3 qt desktop-application qmainwindow
我有一个QMainWindow由另一个应用程序启动的 。
问题是,在多显示器设置中,启动 my 的应用程序QMainWindow可能驻留在第三个屏幕上,但我的窗口将始终在第一个屏幕上启动。
我通过以下方式解决了这个问题......
QDesktopWidget *m = new QDesktopWidget();
QPoint p= QCursor::pos();
int r= m->screenNumber(p); //get the screennumber where the mouse is
QRect d=m->screenGeometry(r);
QPoint l = d.center(); //not the correct solution
mainWin->move(l); //move the window to that screen
mainWin->show(); //launch
Run Code Online (Sandbox Code Playgroud)
现在,我如何在屏幕中央启动这个窗口。d.center()不是正确的方法,因为窗口的左上角将从中心点启动,所以它会被遮挡。
好心提醒。
也许尝试这样的事情:
void MainWindow::CenterToScreen(QWidget* widget) {
if (!widget)
return;
QDesktopWidget* m = QApplication::desktop();
QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos()));
int desk_x = desk_rect.width();
int desk_y = desk_rect.height();
int x = widget->width();
int y = widget->height();
widget->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top());
}
Run Code Online (Sandbox Code Playgroud)
和用法:
CenterToScreen(this); // or CenterToScreen(mainWin);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5345 次 |
| 最近记录: |