Results 1 to 3 of 3

Thread: Custom QWindow add widgets

  1. #1
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Question Custom QWindow add widgets

    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.

    Qt Code:
    1. #include <QtWidgets>
    2. class MyWindow: public QWindow
    3. {
    4. public:
    5. MyWindow():
    6. m_update_pending(false)
    7. {
    8. m_backingStore = new QBackingStore(this);
    9. setTitle("QWindow Background Image");
    10. resize(300,250);
    11. }
    12.  
    13. bool event(QEvent *event)
    14. {
    15. if (event->type() == QEvent::UpdateRequest)
    16. {
    17. m_update_pending = false;
    18. renderNow();
    19. return true;
    20. }
    21. return QWindow::event(event);
    22. }
    23.  
    24. void renderLater()
    25. {
    26. if (!m_update_pending)
    27. {
    28. m_update_pending = true;
    29.  
    30. //Post this event to event queue for later processing
    31. QCoreApplication::postEvent(this,
    32. new QEvent(QEvent::UpdateRequest));
    33. }
    34. }
    35.  
    36. void resizeEvent(QResizeEvent *resizeEvent)
    37. {
    38. m_backingStore->resize(resizeEvent->size());
    39. if (isExposed())
    40. {
    41. renderNow();
    42. }
    43. }
    44.  
    45. void exposeEvent(QExposeEvent *)
    46. {
    47. if (isExposed())
    48. {
    49. renderNow();
    50. }
    51. }
    52.  
    53. void renderNow()
    54. {
    55. if (!isExposed())
    56. return;
    57.  
    58. QRect rect(0, 0, width(), height());
    59. m_backingStore->beginPaint(rect);
    60.  
    61. QPaintDevice *device = m_backingStore->paintDevice();
    62. QPainter painter(device);
    63.  
    64. painter.drawPixmap(rect,QPixmap("background.jpg"));
    65. painter.drawText(QRectF(0, 0, width(), height()),
    66. Qt::AlignCenter, QStringLiteral("QWindow"));
    67.  
    68. m_backingStore->endPaint();
    69. m_backingStore->flush(rect);
    70. }
    71.  
    72. private:
    73. QBackingStore *m_backingStore;
    74. bool m_update_pending;
    75. };
    76.  
    77. int main(int argc, char *argv[])
    78. {
    79. QApplication app(argc, argv);
    80.  
    81. MyWindow window;
    82. window.show();
    83.  
    84. return app.exec();
    85. }
    To copy to clipboard, switch view to plain text mode 

    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/piperma...ly/004833.html
    [2]:http://codeprogress.com/cpp/librarie...ackgroundImage

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QWindow add widgets

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Tuxedo (22nd March 2017)

  4. #3
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Custom QWindow add widgets

    Quote Originally Posted by anda_skoa View Post
    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:

    Qt Code:
    1. include <QtWidgets/QApplication>
    2. include "mainwindow.h"
    3. include <QWindow>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. QWindow *window = w.windowHandle();
    11. window->reportContentOrientationChange(Qt::LandscapeOrientation);
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Is this the right way to access the associated QWindow from my MainWindow? However this is working.

Similar Threads

  1. example using Qwindow
    By PstdEr in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2013, 13:31
  2. Replies: 1
    Last Post: 18th July 2012, 09:59
  3. Replies: 0
    Last Post: 30th April 2012, 15:17
  4. Replies: 0
    Last Post: 15th May 2009, 15:38
  5. Parenting QWindow...
    By roomie in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 09:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.