3

我想在屏幕右侧显示我的主窗口。

我使用这段代码:

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

我收到这个错误:

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

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

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

4

2 回答 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());
于 2013-08-16T07:30:59.160 回答
0

假设有问题的窗口是 800×800:

QRect rec = QApplication::desktop()->availableGeometry();
move(QPoint((rec.width()-800)/2, (rec.height()-800)/2));
于 2016-09-19T21:22:38.407 回答