【发布时间】:2019-02-12 14:51:01
【问题描述】:
在 windows 平台上开发基于平板电脑的 qt 应用程序。根据我们的要求,需要将应用程序修复为横向模式,但无法修复。我们使用的是 QMainWindow。
参考了几个链接来解决问题,但没有奏效。
Reference1 , Reference2 : 通过覆盖函数尝试。
Reference3 :也在我们的 qt 应用程序中尝试过,但没有成功。
以下代码是我们的示例代码:
mainwindow.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;
};
main.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();
}
mainwindow.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 每当我们将窗口显示方向模式更改为纵向时,我们可以看到应用程序更改为纵向模式(正常行为),即使我们将系统方向更改为纵向我们的窗口也是如此应用程序应仅处于横向模式。