Add QML objects to your C++ widget application.



  • My company's High Command has ordered me to develop a tumbler.

    This tumbler or rrrrrrrr-thing as we call it is a QML type object. And I have no experience with QML. I have already googled some stuff but I can't find how to add a QML object to the rest of my application.

    I do know:

    • I have to use a QGraphicView widget. I already have one of those in use with some extra space left. (I'm using Qt 5.8)
    • I must include: #include <QtQuick/QQuickView> and #include <QtQml/QQmlEngine> in the header file
    • I must make a new QML file.

    What I don't know:

    • how can I make a QML object in my cpp file and give it a place on my QGraphicView?




  • Hm so far for the need of a QGraphicView :)

    I have done:

    • added to header.
    #include <QtQuickWidgets/QQuickWidget>
    
    • added to header
    QQuickWidget *view;
    
    
    • added to setup of CPP file
    view = new QQuickWidget;
    view->setSource(QUrl::fromLocalFile("Tumbler.qml"));
    view->show();
    

    But I'm having error messages

    mainwindow.cpp:-1: error: undefined reference to `QQuickWidget::QQuickWidget(QWidget*)'
    mainwindow.cpp:-1: error: undefined reference to `QQuickWidget::setSource(QUrl const&)'
    mainwindow.cpp:-1: error: undefined reference to `QQuickWidget::QQuickWidget(QWidget*)'
    mainwindow.cpp:-1: error: undefined reference to QQuickWidget::setResizeMode(QQuickWidget::ResizeMode)'
    :-1: error: collect2: error: ld returned 1 exit status
    

    Did I include the wrong library or something????

    BTW: I also added a QQuickWidget to the ui which is named 'view'


  • Moderators

    @bask185 said in Add QML objects to your C++ widget application.:

    QQuickWidget

    Did you add

    QT += quickwidgets
    

    to your pro file as is stated here http://doc.qt.io/qt-5/qquickwidget.html ?



  • Ok that solved the error problems. I don't think I will forget this a 3rd time @jsulm ;)

    I also ditched the code I had and I moved the QML file to the resource folder. Using Qt creator's convenient style sheet bar thing (that bar in the UI on the right with all the parameters) I selected the QML file and I got me a working Tumbler \o/

    I am only far removed from the part in which this tumbler is going to be but... minor detail


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.