Results 1 to 6 of 6

Thread: QT5.6 Howto load qml into QMainWindow derived userdefined Object

  1. #1
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QT5.6 Howto load qml into QMainWindow derived userdefined Object

    I am trying to embed a simple "Hello World" text qml into a QMainWindow derived userdefined object.
    In any possible idea that i tested, the text does not show up.

    This is my object constructor code:
    Qt Code:
    1. MainWindow::MainWindow() : QMainWindow ()
    2. {
    3. this->resize(1024, 768);
    4.  
    5. QWidget *topFiller = new QWidget;
    6. topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    7.  
    8. QWidget *bottomFiller = new QWidget;
    9. bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    10.  
    11. QVBoxLayout *layout = new QVBoxLayout;
    12.  
    13. layout->setMargin(5);
    14. layout->addWidget(topFiller);
    15. layout->addWidget(bottomFiller);
    16. this->setLayout(layout);
    17.  
    18. createActions();
    19. createMenus();
    20.  
    21. QQmlEngine engine(this);
    22. QQmlComponent component(&engine, QUrl::fromLocalFile("D:\\test.qml"),this);
    To copy to clipboard, switch view to plain text mode 

    This is my main app code:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow mw;
    5. mw.show();
    6.  
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    This ist the QML file:
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Text {
    4. text: "Hello world!" //a basic greeting
    5. /*
    6.   We want this text to stand out from the rest so
    7.   we give it a large size and different font.
    8.   */
    9. font.family: "Helvetica"
    10. font.pointSize: 24
    11. }
    To copy to clipboard, switch view to plain text mode 

  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: QT5.6 Howto load qml into QMainWindow derived userdefined Object

    Couple of things:

    1) QMainWindow has a layout by default, if you need to add content you create a widget and set it as the main window's centralWidget

    2) You are loading the QML file into the QQmlComponent but you never create an object.

    3) The root element of your QML file is a QQuickItem. A QQuickItem is not a widget.

    Cheers,
    _

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

    froeben (12th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT5.6 Howto load qml into QMainWindow derived userdefined Object

    Thanks for the input!
    Could you explain more detailed the third remark?

    3) The root element of your QML file is a QQuickItem. A QQuickItem is not a widget.

    Is it possible to add a QQuickItem to a widget?

    Kind regards,
    Frank

  5. #4
    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: QT5.6 Howto load qml into QMainWindow derived userdefined Object

    The easiest way to embed a QtQuick scene into a widget is to use QQuickWidget.

    Cheers,
    _

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

    froeben (12th February 2016)

  7. #5
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT5.6 Howto load qml into QMainWindow derived userdefined Object

    So, after one and a half day, i found 2 possible solutions that work which i would like to share with other newbies:
    Qt Code:
    1. MainWindow::MainWindow() : QMainWindow ()
    2. {
    3. // embed resources into binary
    4. // infos: http://doc.qt.io/qt-5/resources.html
    5. Q_INIT_RESOURCE(qmlsources);
    6.  
    7. this->resize(1024, 768);
    8.  
    9. QWidget *topFiller = new QWidget;
    10. topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    11.  
    12. QWidget *bottomFiller = new QWidget;
    13. bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    14.  
    15. QVBoxLayout *layout = new QVBoxLayout;
    16.  
    17. layout->setMargin(5);
    18. layout->addWidget(topFiller);
    19. //layout->addWidget(_infoLabel);
    20. layout->addWidget(bottomFiller);
    21. //this->setLayout(layout);
    22.  
    23. createActions();
    24. createMenus();
    25.  
    26. //WORKS!!!
    27. // uses QQmlEngine and QQuickWidget
    28. QQmlEngine *engine = new QQmlEngine(this);
    29. QQuickWidget *view = new QQuickWidget(engine, this);
    30.  
    31. view->setSource(QUrl("qrc:/test.qml"));
    32. this->setCentralWidget(view);
    33.  
    34. /* //WORKS!!!
    35.   // uses QQuickView and QWidget
    36. QQuickView *view = new QQuickView(QUrl("qrc:/test.qml"));
    37. QWidget *container = QWidget::createWindowContainer(view);
    38. this->setCentralWidget(container);
    39. */
    40. }
    To copy to clipboard, switch view to plain text mode 

    Since this is a library, you have to embed the qml-file in the resources otherwise the application will not find you qml-file since it is located in the directory tree of the library.

    To be honest, the information are described in the documentation but hard to understand since the examples only show snippets of the code.

  8. #6
    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: QT5.6 Howto load qml into QMainWindow derived userdefined Object

    If you intend this to be useful for others, you should probably clean it up

    There is lots of unneccessary code, e.g. the filler widgets and layout.

    Also you don't have to create an QQmlEngine for QQuickWidget, it can do that itself just like QQuickView.

    I am also unsure what you mean with your statement regarding resources.
    Q_INIT_RESOURCE is only necessary if the resource is in a static library.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 15th July 2011, 18:31
  2. Setting border for QGLWidget derived object
    By fisyher84 in forum Newbie
    Replies: 1
    Last Post: 4th March 2011, 05:36
  3. Deleting a QWidget derived object manually
    By hubbobubbo in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 16:32
  4. Can't add an object derived from QWidget in a layout.
    By Netich in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2010, 21:55
  5. How to call QValidator derived object
    By bruccutler in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2007, 17:07

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.