PDA

View Full Version : Custom QWindow add widgets



Tuxedo
17th March 2017, 16:13
I have a qt application which uses QGraphicsView and QGraphicsProxyWidget to rotate my widgets. Now I need to tell the system that my orientation has changed.
This can only be done by subclassing QWindow and do the following (view is a QWindow) [1]:

view.reportContentOrientationChange(Qt::LandscapeO rientation);
view.screen()->setOrientationUpdateMask(Qt::LandscapeOrientation) ;

QWindow is for me new so I was looking for an example and I found this [2].
Now I would like to know how to add widgets to this qwindow example.
I tried many things because I don't know a way to achieve this but was not successful.
Subclassing a qwindow is mostly done for gl or sdl stuff but not for widgets.


#include <QtWidgets>
class MyWindow: public QWindow
{
public:
MyWindow():
m_update_pending(false)
{
m_backingStore = new QBackingStore(this);
setTitle("QWindow Background Image");
resize(300,250);
}

bool event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest)
{
m_update_pending = false;
renderNow();
return true;
}
return QWindow::event(event);
}

void renderLater()
{
if (!m_update_pending)
{
m_update_pending = true;

//Post this event to event queue for later processing
QCoreApplication::postEvent(this,
new QEvent(QEvent::UpdateRequest));
}
}

void resizeEvent(QResizeEvent *resizeEvent)
{
m_backingStore->resize(resizeEvent->size());
if (isExposed())
{
renderNow();
}
}

void exposeEvent(QExposeEvent *)
{
if (isExposed())
{
renderNow();
}
}

void renderNow()
{
if (!isExposed())
return;

QRect rect(0, 0, width(), height());
m_backingStore->beginPaint(rect);

QPaintDevice *device = m_backingStore->paintDevice();
QPainter painter(device);

painter.drawPixmap(rect,QPixmap("background.jpg"));
painter.drawText(QRectF(0, 0, width(), height()),
Qt::AlignCenter, QStringLiteral("QWindow"));

m_backingStore->endPaint();
m_backingStore->flush(rect);
}

private:
QBackingStore *m_backingStore;
bool m_update_pending;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MyWindow window;
window.show();

return app.exec();
}

Do I need to use a window container? What would be the best way? I have no clue, no starting point, I am lost.:(
I would be very happy if someone could make a small example showing a qpushbutton or some other widget on that window.
Other hints and tips are also welcome.

[1]:https://lists.sailfishos.org/pipermail/devel/2014-July/004833.html
[2]:http://codeprogress.com/cpp/libraries/qt/showQtExample.php?index=566&key=QWindowBackgroundImage

anda_skoa
18th March 2017, 09:29
A top level widget (without a parent) or one of the widget window types (QMainWindow, QDialog) has an associated QWindow.

Can you explain why you need to derive a subclass from QWindow?

Cheers,
_

Tuxedo
22nd March 2017, 09:58
A top level widget (without a parent) or one of the widget window types (QMainWindow, QDialog) has an associated QWindow.

Thank you for that explanation it is good to know.
After a hint from elros I solved my problem by doing this:



include <QtWidgets/QApplication>
include "mainwindow.h"
include <QWindow>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QWindow *window = w.windowHandle();
window->reportContentOrientationChange(Qt::LandscapeOrient ation);
return a.exec();
}

Is this the right way to access the associated QWindow from my MainWindow? However this is working.