Fix the Qt application orientation to landscape - c++

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.

Related

change theme with radio button Qwidget in Qt C++

I want to change my application Theme with QRadioButton in the QWidget application.
can anyone help me?
I have a Setting class that has some settings and one of them is changing the theme between dark and light.
it is a C++ app with OpenGL.
void Settings::on_DarkTheme_clicked()
{
}
void Settings::on_lightTheme_clicked()
{
}
what should I have to write in these 2 methods?
I used .qss file like this
int main(int argc, char *argv[])
{
QSurfaceFormat fmt;
fmt.setVersion(3,3);
fmt.setProfile(QSurfaceFormat::CoreProfile);
fmt.setSamples(8);
fmt.setOption(QSurfaceFormat::StereoBuffers);
fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
QSurfaceFormat::setDefaultFormat(fmt);
loadSettings();
App app(argc, argv);
QFile stylesheetFile("./Combinear.qss");
stylesheetFile.open(QFile::ReadOnly);
QString stylesheet = QLatin1String(stylesheetFile.readAll());
app.setStyleSheet(stylesheet);
MainWindow w;
w.show();
const auto returnValue = app.exec();
App::setFinished();
return returnValue;
}
but it didn't Work and I'm confused
First of all, I add a radioButton in my mainwindow.ui .
this is my mainwindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void changedDark();
void changedLight();
private slots:
void on_radioButton_clicked(bool checked);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
and my mainwindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_radioButton_clicked(bool checked)
{
if (checked)
{
ui->radioButton->setText("Dark");
emit changedDark();
}
else
{
ui->radioButton->setText("Light");
emit changedLight();
}
}
and my main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(&w, &MainWindow::changedDark, &a, [&]
{
/**
* Load the application style
*/
QFile dark_styleFile(":/theme/themes/MaterialDark.qss");
dark_styleFile.open(QFile::ReadOnly);
/**
* Apply the loaded stylesheet
*/
QString style(dark_styleFile.readAll());
a.setStyleSheet(style);
});
QObject::connect(&w, &MainWindow::changedLight, &a, [&]
{
/**
* Load the application style
*/
QFile light_styleFile(":/theme/themes/Ubuntu.qss");
light_styleFile.open(QFile::ReadOnly);
/**
* Apply the loaded stylesheet
*/
QString style(light_styleFile.readAll());
a.setStyleSheet(style);
});
return a.exec();
}
This is my result:
you can clone it from this repo:
https://github.com/parisa-hr/qt-theme-example

QT: cannot control my QGraphicsPixmapItem with keyboard even after setfocus()

I am making a game requiring qgraphicspixmapitem moving around in the view.
Now, I plan to move my QGraphicsPixmapitem up and down with my keyboard. It works perfectly fine initially.
But since my scene's and view's size are unequal then, which looks pretty wired. So I added some functions to adjust my scene and view's size to be equal.
However, after setting my QGraphicsScene and QGraphicsView same size,
I cannot move my qgraphicspixmapitem with my keyboard anymore.
I've tried adding setFocus, Flag to enable my qgraphicspimapitem stay controlled by my keyboard, but in vain.
Any idea will be very appreciated. Thank you!
main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//scene
scene = new QGraphicsScene(0, 0, 1050, 600);
//player
player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(100,100));
player->setFlag(QGraphicsPixmapItem:: ItemIsFocusable,true);
player->setFocus();
player->setPos(350, 500);
scene->addItem(player);
playertimer->start(10);
//view
view = new QGraphicsView(this);
view ->setScene(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCentralWidget(view);
view->setFixedSize(1055,605);
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Down){
player->setPos(player->x(),player->y()+10);
}
else if (e->key() == Qt::Key_Up){
player->setPos(player->x(),player->y()-10);
}
}
}
main window.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QKeyEvent>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
virtual void keyPressEvent(QKeyEvent *e);
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsItem *player;
QGraphicsView * view;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
You have never been able to move the item, what you have done is to move the scrollbars making the same effect in the visual part.
What you have to do is use the KeyPress event of the QGraphicsView, one way to do it is to create a new class that inherits from QGraphicsView, but another simpler way is to install an event filter as shown below:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//scene
scene = new QGraphicsScene(0, 0, 1050, 600);
//player
player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(100,100));
player->setPos(350, 500);
scene->addItem(player);
//view
view = new QGraphicsView;
view ->setScene(scene);
view->installEventFilter(this);
setCentralWidget(view);
view->setFixedSize(1055,605);
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == view && event->type() == QEvent::KeyPress){
QKeyEvent *kevent = static_cast<QKeyEvent *>(event);
if(kevent->key() == Qt::Key_Down){
player->setPos(player->pos() + QPointF(0, 10));
return true;
}
else if(kevent->key() == Qt::Key_Up){
player->setPos(player->pos() - QPointF(0, 10));
return true;
}
}
return QMainWindow::eventFilter(watched, event);
}
MainWindow::~MainWindow()
{
delete ui;
}
In the following link you can find the complete example

QMdiArea is returning the wrong height and width

I am trying to arrange my subWindows in the QMdiArea vertically. I saw lot of examples online and they all were doing the same thing as I am doing here.
I have two textEdits which needs to be tiled vertically both covering half of the screen. So in the constructor of the MainWindow I add the two textEdits as subWindow to the qMdiArea and then find the height and width of the qMdiArea divide the height by 2 and resize the subWindow. Please see the code below.
My mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
qMdiArea = new QMdiArea();
qTextEdit1 = new QTextEdit();
qTextEdit2 = new QTextEdit();
setCentralWidget(qMdiArea);
qMdiArea->adjustSize();
qMdiArea->addSubWindow(qTextEdit1);
qMdiArea->addSubWindow(qTextEdit2);
QPoint position(0, 0);
foreach (QMdiSubWindow *window, qMdiArea->subWindowList())
{
QRect rect(0, 0, qMdiArea->width(), qMdiArea->height() / qMdiArea->subWindowList().count());
window->setGeometry(rect);
window->move(position);
position.setY(position.y() + window->height());
}
}
MainWindow::~MainWindow()
{
delete ui;
}
My window.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMdiArea>
#include <QTextEdit>
#include <QPoint>
#include <QMdiSubWindow>
#include <QRect>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QMdiArea *qMdiArea;
QTextEdit *qTextEdit1;
QTextEdit *qTextEdit2;
};
#endif // MAINWINDOW_H
and my Main File :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
But its not happening as expected. The window just occupy a part of the screen though they are tiled vertically. My screen resolution is 1920x1200
The height() and width() of mdiArea are invalid at that stage, because the widget hasn't been exposed/shown yet. Calling show() only schedules a widget for display, the act of sizing it and showing it on screen happens later when the control has returned to the event loop.
As a solution, you can override the resizeEvent handler. Once you do, your project will work again:
Definition in mainwindow.h:
virtual void resizeEvent(QResizeEvent *ev) override;
Implementation in mainwindow.cpp:
void MainWindow::resizeEvent(QResizeEvent *ev)
{
Q_UNUSED(ev)
QPoint position(0, 0);
foreach (QMdiSubWindow *window, qMdiArea->subWindowList())
{
QRect rect(0, 0, qMdiArea->contentsRect().width(), qMdiArea->contentsRect().height() / qMdiArea->subWindowList().count());
window->setGeometry(rect);
window->move(position);
position.setY(position.y() + window->height());
}
}
Also it seems that you don't really need to have this->showMaximized(); call inside MainWindow's constructor. You can call it from main.cpp, for example.

QComboBox currentIndex() to hide other widgets

What it should do: When the QComboBox is on the first item (index 0) it should hide the QStackedWidget. Which causes the QComboBox to extend as much as possible. As soon as you change the item in the QComboBox to anything, the QComboxBox should shrink and the QStackedWidget should display the correct page.
What I have and what it does instead: I test what the current index item is of the QComboBox and depending on that I change the size policies and visibility of widgets in order to obtain what I want. But it doesn't work. I tried to do workarounds but I cant seem to figure this out.
I also used qDebug() to see what currentIndexItem returns, and it seems to be stuck at 0, no matter to what index I change the QComboBox to. Do I have to update the currentItemIndex?
Note: I have a signal connected in the designer from QComboBox to QStackedWidget: currentIndexChanged(int) -> setCurrentIndex(int)
Here is my code.
My code:
interfacewindow.h
#ifndef INTERFACEWINDOW_H
#define INTERFACEWINDOW_H
#include <QMainWindow>
#include <QPainter>
#include <QComboBox>
namespace Ui
{
class InterfaceWindow;
}
class InterfaceWindow : public QMainWindow
{
Q_OBJECT
public:
explicit InterfaceWindow(QWidget *parent = 0);
~InterfaceWindow();
private:
Ui::InterfaceWindow *ui;
private slots:
void on_actionClose_triggered();
};
#endif // INTERFACEWINDOW_H
interfacewindow.cpp
#include "interfacewindow.h"
#include "ui_interfacewindow.h"
#include <QDebug>
InterfaceWindow::InterfaceWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::InterfaceWindow)
{
ui->setupUi(this);
if(ui->comboBox->currentIndex() == 0)
{
ui->comboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
ui->stackedWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
ui->stackedWidget->setVisible(false);
}
else
{
ui->comboBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
ui->stackedWidget->setVisible(true);
}
}
InterfaceWindow::~InterfaceWindow()
{
delete ui;
}
void InterfaceWindow::on_actionClose_triggered()
{
this->close();
}
main.cpp
#include <QApplication>
#include "interfacewindow.h"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
InterfaceWindow w;
w.show();
return a.exec();
}

Frameless and transparent window qt5

I'm quite new to qt and c++ and I've encountered a problem that I can't seem to figure out. I'm wanting to open a frameless and transparent window when I click a button on the main ui. I've got the code working to open a new window when I press a button on the main ui but I can't seem to get the frameless and transparent part working.
Here is the source codes for the small program that I wrote to learn this
main.cpp
#include "learnwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LearnWindow w;
w.show();
return a.exec();
}
Here is the LearnWindow.h
#ifndef LEARNWINDOW_H
#define LEARNWINDOW_H
#include <QMainWindow>
#include <transwindow.h>
namespace Ui {
class LearnWindow;
}
class LearnWindow : public QMainWindow
{
Q_OBJECT
public:
explicit LearnWindow(QWidget *parent = 0);
~LearnWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::LearnWindow *ui;
TransWindow *winTrans;
public slots:
void openTrans();
};
#endif // LEARNWINDOW_H
Here is learnwindow.cpp
#include "learnwindow.h"
#include "ui_learnwindow.h"
LearnWindow::LearnWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LearnWindow)
{
ui->setupUi(this);
}
LearnWindow::~LearnWindow()
{
delete ui;
}
void LearnWindow::openTrans()
{
winTrans = new TransWindow (this);
//winTrans->setWindowTitle("NewWin");
// winTrans->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
//winTrans->setAttribute(Qt::WA_TranslucentBackground,true);
//winTrans->setAutoFillBackground(false);
//winTrans->setStyleSheet("background:transparent;");
winTrans->show();
}
void LearnWindow::on_pushButton_clicked()
{
openTrans();
}
Here is the transwindow.h
#ifndef TRANSWINDOW_H
#define TRANSWINDOW_H
#include <QDialog>
namespace Ui {
class TransWindow;
}
class TransWindow : public QDialog
{
Q_OBJECT
public:
explicit TransWindow(QWidget *parent = 0);
//setWindowFlags(windowFlags()| Qt::FramelessWindowHint);
~TransWindow();
private:
Ui::TransWindow *ui;
};
#endif // TRANSWINDOW_H
And here is transwindow.cpp
#include "transwindow.h"
#include "ui_transwindow.h"
TransWindow::TransWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TransWindow)
{
//setWindowTitle("NewWin");
//setWindowFlags(Qt::FramelessWindowHint);
//setAttribute(Qt::WA_TranslucentBackground,true);
ui->setupUi(this);
}
TransWindow::~TransWindow()
{
delete ui;
}
In the different source codes you'll see commented out lines which is the different things that I've tried. For the most part the problem is, if I un-comment out any of the lines that try to set the "Qt::FramlessWindowHint" the program runs normally but never opens a new window when I click the button on the main ui.
If I un-comment out any of the lines where I set the "Qt::WA_TranslucentBackground" the new window will open up when when the button is pressed in the main ui but the background of the new window is black, not transparent.
Other info that may be pertinent:
linux: ubunto 12.04
qt 5.0.2 (64-bit)
qt creator 2.7.1
Try this:
setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
setParent(0); // Create TopLevel-Widget
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_PaintOnScreen); // not needed in Qt 5.2 and up