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.
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.