Lock app orientation to landscape in Qt

梦想的初衷 提交于 2019-12-25 03:14:23

问题


I would like to lock my app developed with Qt to landscape orientation, even if the screen display is portrait. I have tried adding to my code the resizeEvent method found here: http://qt-project.org/doc/qt-4.8/widgets-orientation.html, but my app still does not display correctly. Here is my code for resizeEvent:

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QSize size = event->size();
    qDebug() << size;
    bool isLandscape = size.width() > size.height();

    if (isLandscape == false){
        size.transpose();
    }

    this->setFixedSize(size);
}

Does anyone know how to do this in Qt 4.8.5? I am trying to display an app for a 320x240 display.

Thanks


回答1:


You can't really follow that example. It shows two different widgets depending on the orientation. Furthermore the doc warns about modifying size properties inside resizeEvent.

One solution would be to set a fix aspect ratio similar to 320x240 by overloading QWidget::heightForWidth. You wouldn't need to overload resizeEvent.

It will look like

int MainWindow::heightForWidth( int w ) { 
     return (w * 240 )/320; 
}

heightForWidth is discussed in detail in https://stackoverflow.com/a/1160476/1122645.

edit:

sizeHint is used by the layout of the parent to compute the size of children widgets. In general, it make sense to implement it as

QSize MainWindow::sizeHint() const
{
    int w = //some width you seem fit
    return QSize( w, heightForWidth(w) );
}

but if MainWindow is top level then it will not change anything. You can also alter heightForWidth flag of your current size policy

QSizePolicy currPolicy = this->sizePolicy();
currPolicy->setHeightForWidth(true);
this->SetSizePolicy(currPolicy); 

But again, if it is a top level widget I doesnt change much.

Anyway you don't need to call updateGeometry.



来源:https://stackoverflow.com/questions/29005539/lock-app-orientation-to-landscape-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!