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

C++ 如何在Qt 5.1.0中将主窗口与屏幕右侧对齐,c++,qt,alignment,screen,C++,Qt,Alignment,Screen,此代码正在运行: QRect r = QApplication::desktop()->availableGeometry(); QRect main_rect = this->geometry(); main_rect.moveTopRight(r.topRight()); this->move(main_rect.topLeft()); 此代码在屏幕的位置上工作。。但我想在屏幕右侧对齐 你知道吗 谢谢。我认为您应该在主布局中使用setAlignment,例如 QHBox

此代码正在运行:

QRect r = QApplication::desktop()->availableGeometry();
QRect main_rect = this->geometry();
main_rect.moveTopRight(r.topRight());
this->move(main_rect.topLeft()); 
此代码在屏幕的位置上工作。。但我想在屏幕右侧对齐

你知道吗


谢谢。

我认为您应该在主布局中使用setAlignment,例如

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

我有一个解决办法,但遗憾的是,它不是那么容易

首先,你必须在短暂的延迟后做这个动作,你可以阅读更多关于它的信息

要移动窗口,您必须执行以下操作:

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();
}
我添加了一张图片,使我上面所做的事情形象化。我希望我能帮助你

编辑:刚刚看到这个问题已经一年多了。。。也许这对某人还是有帮助的


转到“设计”并单击主窗口上的某个位置,然后在“属性”窗口中找到“布局方向”,并将其更改为“右到左”