0

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

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

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

参考 3:也在我们的 qt 应用程序中尝试过这个,但没有奏效。

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

主窗口.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;
};

主文件

#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();
}

主窗口.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) );
}

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

4

1 回答 1

0

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

以下是如何读取 Windows 当前旋转。

于 2019-02-13T12:46:30.540 回答