在屏幕右侧显示主窗口 (Qt 5.1.0)

3

我希望把我的主窗口显示在屏幕的右侧。

我使用了这段代码:

QRect r  = this->frameGeometry();   
r.moveRight(QDesktopWidget::availableGeometry());   
this->move(r.topRight());

我收到了这个错误:

错误:无法在没有对象的情况下调用成员函数'const QRect QDesktopWidget::availableGeometry(int) const'

如果我使用1024代替QDesktopWidget::availableGeometry(),它可以工作...但我不想静态初始化...

如何动态地为不同的屏幕尺寸重新定位窗口?

2个回答

3

QDesktopWidget::availableGeometry 不是一个静态函数。您可以使用QApplication::desktop() 函数获取一个QDesktopWidget对象:

QRect r  = this->frameGeometry();
r.moveRight(QApplication::desktop()->availableGeometry()); 

您需要在moveRight()函数中放置其他内容。您不能在这里放置一个QRect。也许您想要做的是:

QRect r = QApplication::desktop()->availableGeometry();
r.setLeft(r.center().x());
this->resize(r.width(), r.height());
this->move(r.topLeft());

或者,如果您不想调整窗口大小:

QRect r = QApplication::desktop()->availableGeometry();
QRect main_rect = this->geometry();
main_rect.moveTopRight(r.topRight());
this->move(main_rect.topLeft());

我尝试了一下,但是收到了这个错误:错误:调用 'QRect :: moveRight(const QRect)' 没有匹配的函数 - MOC89
非常感谢,它已经起作用了 ;) - MOC89
我们正在处理窗口的位置。但现在我想将其右对齐屏幕 :/ - MOC89
@MOC89 你是什么意思? - thuga
我已经为这个问题工作了四天 :(( .. 我想要实现这样的效果; 如果我点击任何按钮,我希望MainWindow像İMAGE_2那样扩展,就像İMAGE_1一样。 - MOC89
显示剩余3条评论

0
假设所讨论的窗口大小为800×800:
QRect rec = QApplication::desktop()->availableGeometry();
move(QPoint((rec.width()-800)/2, (rec.height()-800)/2));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接