1

Developing a tablet based qt application in windows platform. As per our requirement need to fix the application to landscape mode but not able to fix that. We were using QMainWindow.

Referred few links to fix the issue but didn't worked.

Reference1 , Reference2 : Tried by overriding functions.

Reference3 : Also tried this in our qt application, but not worked.

Below code is our sample code:

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

Can anyone help me on fixing this. Please let me know if am doing anything wrong in above code.

Srujan B
  • 43
  • 8
  • I don't know what's the problem, but did you try the Orientation example of Qt and did it work? Otherwise, an Android solution would be to put the orientation into your AndroidManifest.xml or [call a Java function using a QAndroidJniObject](https://stackoverflow.com/questions/28643633/changing-orientation-of-a-particular-page-in-android). – taminob Feb 12 '19 at 16:10
  • Your requirement to "fix application to landscape mode" needs more refinement before suggesting an answer. IF the display is in portrait orientation, does the app not display? Does it only display in part of the screen? Does it rotate itself so that it is displaying in landscape? (Rotate left or right to portrait?). – jwernerny Feb 12 '19 at 18:38
  • @jwernerny Whenever we change the windows display orientation mode to portrait, we can see the application change to portrait mode(in normal behavior) and the expectation is even if we change the system orientation to portrait our windows application should be in landscape mode only. – Srujan B Feb 13 '19 at 05:17

1 Answers1

0

Had the same problem and unfortunately, the best solution was just to disable rotation in Windows settings. I doubt there is a reasonable way to do that from application but i'am not 100% sure. Only other solution i found was to draw your view according to Windows rotation (so the view would be drawn rotated).

Here is how to read Windows current rotation.

Community
  • 1
  • 1
Piotr S
  • 41
  • 6
  • Thanks for the answer.But i am not sure why am i receiving answers on how to view on orientation changes.Anyways, i tried the code in the link given. does not work as per my requirement.What concern I have is- How to restrict the application orientation and Fix it to Landscape mode only, even when the device is rotated or screen orientation is changed deliberately. My application should display itself only in Landscape mode and NOT in portrait mode.Or Is there a way to fix the auto-rotation of device pro-grammatically for windows? – Srujan B Feb 14 '19 at 12:35