【问题标题】:Fix the Qt application orientation to landscape将 Qt 应用程序方向修复为横向
【发布时间】:2019-02-12 14:51:01
【问题描述】:

在 windows 平台上开发基于平板电脑的 qt 应用程序。根据我们的要求,需要将应用程序修复为横向模式,但无法修复。我们使用的是 QMainWindow。

参考了几个链接来解决问题,但没有奏效。

Reference1 , Reference2 : 通过覆盖函数尝试。

Reference3 :也在我们的 qt 应用程序中尝试过,但没有成功。

以下代码是我们的示例代码:

ma​​inwindow.h

#include <QMainWindow>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    int heightForWidth(int w) const;
    QSize sizeHint() const;

private:
    Ui::MainWindow *ui;
};

ma​​in.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QDebug>

#include "windows.h"
#include "qt_windows.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

//    typedef BOOL (WINAPI* SETAUTOROTATION)(BOOL bEnable);

//    SETAUTOROTATION SetAutoRotation = (SETAUTOROTATION)GetProcAddress(GetModuleHandle(TEXT("user32.dll")),
//                                                                      (LPCSTR)2507);
//    if (SetAutoRotation != nullptr)
//    {
//        qDebug() << "bEnable: " << SetAutoRotation;
//        SetAutoRotation(FALSE);
//    }

//    qDebug() << "bEnable: " << SetAutoRotation;

    MainWindow w;

    w.showMaximized();
    w.show();

    return a.exec();
}

ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "windows.h"
#include "qt_windows.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QSizePolicy currPolicy = this->sizePolicy();
    currPolicy.setHeightForWidth(true);
    this->setSizePolicy(currPolicy);

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

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

QSize MainWindow::sizeHint() const
{
    int w = 1366;
    return QSize(w, heightForWidth(w) );
}

谁能帮我解决这个问题。如果在上面的代码中做错了什么,请告诉我。

【问题讨论】:

  • 我不知道是什么问题,但是您尝试过 Qt 的 Orientation 示例并成功了吗?否则,Android 解决方案是将方向放入您的 AndroidManifest.xml 或 call a Java function using a QAndroidJniObject
  • 您对“将应用程序修复为横向模式”的要求需要进一步完善,然后才能提出答案。如果显示器是纵向的,应用程序不会显示吗?它只显示在屏幕的一部分吗?它是否会自行旋转以横向显示? (向左或向右旋转到纵向?)。
  • @jwernerny 每当我们将窗口显示方向模式更改为纵向时,我们可以看到应用程序更改为纵向模式(正常行为),即使我们将系统方向更改为纵向我们的窗口也是如此应用程序应仅处于横向模式。

标签: c++ windows qt qt5.11


【解决方案1】:

遇到了同样的问题,不幸的是,最好的解决方案就是在 Windows 设置中禁用旋转。我怀疑是否有合理的方法可以从应用程序中做到这一点,但我不是 100% 确定。 我发现的唯一其他解决方案是根据 Windows 旋转绘制视图(因此视图将被旋转绘制)。

Here is how to read Windows current rotation.

【讨论】:

  • 感谢您的回答。但我不确定为什么我会收到有关如何查看方向更改的答案。无论如何,我尝试了给出的链接中的代码。不能按照我的要求工作。我担心的是-如何限制应用程序方向并将其仅修复为横向模式,即使设备旋转或屏幕方向被故意更改。我的应用程序应该只在横向模式下而不是在纵向模式下显示。或者有没有办法以编程方式修复设备的自动旋转窗口?
猜你喜欢
  • 2012-07-25
  • 1970-01-01
  • 2019-04-17
  • 2018-01-07
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多