0

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

Alvin Dizon
  • 1,855
  • 14
  • 34
  • I dont know for sure, but could it be that after setting the size to fixed, you dont get any further `resizeEvent`? The main difference between your approach and the example is, that you set the fixed size of the mainwindow instead of a (child)widget. – Robert Mar 12 '15 at 09:27

1 Answers1

0

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.

Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Hello, aside from heightForWidth, do I need also need to overload sizeHint()? Based from this link:http://doc.qt.digia.com/qq/qq04-height-for-width.html, I would also need to call updateGeometry as well, am I correct? – Alvin Dizon Mar 13 '15 at 05:22
  • Thanks for clarifying, it seems using sizeHint only applies for widgets inside layouts and not for top level windows like the one for my project. Either I can use heightForWidth and sizeHint on centralWidget or I can use setFixedSize() on the top level MainWindow. – Alvin Dizon Mar 17 '15 at 02:21