[英]How to show QLabel on extended monitor
I have very simple qt application. 我有一个非常简单的Qt应用程序。 My aim is that if I click on a button it should show message on my second/extended monitor. 我的目标是,如果单击按钮,它应该在第二台/扩展显示器上显示消息。 My application would be running on main monitor and display message should appear on second monitor. 我的应用程序将在主监视器上运行,并且显示消息应出现在第二个监视器上。 Thats how my method looks like: 那就是我的方法的样子:
void MainWindow::on_pushButton_clicked()
{
QLabel *ql = new QLabel();
ql->setText("Order is Ready");
ql->move(QGuiApplication::screens().last()->geometry());
ql->show();
.....
}
But this is not working for me.I have also tried but also not working :( Any help would be appreciated? 但这对我不起作用。我也尝试过但也没有工作:(任何帮助将不胜感激吗?
ql->windowHandle()->setScreen(QGuiApplication::screens().last());
This sample doesn't "compile" for me, but try something like this - set coordinates 该示例不会为我“编译”,而是尝试类似的操作-设置坐标
ql->move(QGuiApplication::screens().last()->geometry().width(), QGuiApplication::screens().last()->geometry().height() / 2);
Note: the question is which of your monitor is first and which is last... 注意:问题是显示器的哪一个是第一位,最后一位是...
You need to: 你需要:
Ensure that the window is on the virtual desktop the screen belongs to. 确保窗口位于屏幕所属的虚拟桌面上。
That it is somewhere on the given screen. 它在给定屏幕上的某个位置。
Thus: 从而:
void centerWidgetOnScreen(QWidget *widget, QScreen *screen) {
auto *window = widget->windowHandle();
Q_ASSERT(screen);
Q_ASSERT(window);
window->setScreen(screen);
auto newCenter = screen->geometry().center();
auto rect = window->geometry();
rect.moveCenter(newCenter);
window->setGeometry(rect);
}
void centerWidgetOnLastScreen(QWidget *widget) {
centerWidgetOnScreen(widget, QGuiApplication::screens().last());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.