开发者

C++ Qt window positioning

Does开发者_如何学JAVA Qt has something to offer for the positioning of the tooltip-like windows? (or any types of windows/widgets actually).

I want to be able to update the position of the window automatically, so that it always stays on the screen (or at least fits it as much as possible).

An example of the behaviour I want can be seen in the standard Windows tooltips in the notification area. If the tooltip is big and it has some part of it going off the screen, it gets automatically repositioned.

Obviously, I can write the code myself, but I'm looking for something that has already been written.


I don't know if Qt has one single function that ensures a widget is totally inside of the screen. But with QDesktopWidget it's probably trivial to do.

void function RestrainWidgetToScreen(QWidget * w)
{
    QRect screenRect = QDesktopWidget::availableGeometry(w);
    if(w->frameGeometry().left() < screenRect.left()) {
        w->move(screenRect.left() - w->frameGeometry().left(), 0);
    } else if(w->frameGeometry().right() > screenRect.right()) {
        w->move(screenRect.right() - w->frameGeometry().right(), 0);
    }
    if(w->frameGeometry().top() < screenRect.top()) {
        w->move(0, screenRect.top() - w->frameGeometry().top());
    } else if(w->frameGeometry().bottom() < screenRect.bottom()) {
        w->move(0, screenRect.bottom() - w->frameGeometry().bottom());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜