如何在Qt 5.1.0中将主窗口与屏幕右侧对齐

时间:2013-08-19 14:35:34

标签: c++ qt alignment screen

此代码正常运行:

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

此代码正在处理屏幕位置..但我想对齐到屏幕右侧..

你有什么想法吗?

谢谢..

2 个答案:

答案 0 :(得分:0)

我认为你应该将setAlignment用于你的主要布局

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->setAlignment(Qt::AlignRight);

答案 1 :(得分:0)

我有一个解决方案,但遗憾的是它不是那么容易......

首先,您必须在短暂延迟后进行移动,您可以阅读更多相关信息here (Check the accepted answere)

移动窗口需要做的是:

void MainWindow::timerEvent(QTimerEvent *event)
{

    if(event->timerId() == this->TimerID)
    {
        event->accept();
        this->killTimer(this->TimerID);
        //Used to kill the timer that was created in the constructor, needs the ID to do this

        QRect desktopGeom = QApplication::desktop()->availableGeometry(); //Screen size
        QRect windowFrame = this->frameGeometry(); //Window with frame
        QRect windowSize = this->geometry(); //Window without frame

        int smallFrameFactor = (windowFrame.width() - windowSize.width()) / 2; //Because the left, right and bottom border have the same size
        int wideFrameFactor = windowFrame.height() - (windowSize.height() + smallFrameFactor); //The big, upper border

        int x_pos = desktopGeom.x() + desktopGeom.width() - windowSize.width() - smallFrameFactor;
        int y_pos = desktopGeom.y() + wideFrameFactor;

        this->setGeometry(x_pos, y_pos, windowSize.width(), windowSize.height());
    }
    else
        event->ignore();
}

我添加了一张图片,用来模仿我上面做的事情。我希望我能帮到你。

编辑:刚看到这个问题超过一年了......也许这对某人来说仍然有用......

Example Image