在Qt中将应用程序方向锁定为横向 [英] Lock app orientation to landscape in Qt
问题描述
即使屏幕显示为纵向,我也希望将使用Qt开发的应用锁定为横向.我尝试将在此处找到的resizeEvent方法添加到我的代码中: http://qt-project.org/doc/qt-4.8/widgets-orientation.html ,但是我的应用仍然无法正确显示.这是我的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);
}
有人知道如何在Qt 4.8.5中做到这一点吗?我正在尝试为320x240的显示器显示一个应用程序.
谢谢
您不能真正遵循该示例.它根据方向显示了两个不同的小部件.此外,该文档还警告您修改resizeEvent
内部的size属性.
一种解决方案是通过重载 https://stackoverflow.com/a/1160476/1122645 中进行了详细讨论./p>
父级的 布局 使用
sizeHint
来计算子级窗口小部件的大小.通常,将其实现为
QSize MainWindow::sizeHint() const
{
int w = //some width you seem fit
return QSize( w, heightForWidth(w) );
}
,但是如果MainWindow
是顶级,则它将保持不变.您还可以更改当前尺寸政策的heightForWidth标志
QSizePolicy currPolicy = this->sizePolicy();
currPolicy->setHeightForWidth(true);
this->SetSizePolicy(currPolicy);
但是,如果它是一个顶级小部件,我不会做太多改动.
无论如何,您无需致电updateGeometry
.
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
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
.
这篇关于在Qt中将应用程序方向锁定为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!