Search code examples
qtqmlqtquick2qtwidgets

Can i use quick controls in a qt widgets application?


I need material styled replacements of standard widgets and it's kind of hard to implement them by using custom widgets. I was wondering if there's any way to include widgets from quick control module and use them as regular widgets?


Solution

  • If your application is Qt5.1 and above, the answer is yes you can.

    You have to use the QQuickView object and pass it to static function createWindowContainer of QWidget, which takes QWindow as in paramater.

    QQuickView derived from QQuickWindow which is derived from QWindow.

    So you can pass a QQuickView as an input to the createWindowContainer.

    Below is some rough code.

    //CREATE A QQuickView  OBJECT.
    QQuickView *view = new QQuickView();
    
    //ADD THE QQuickView  OBJECT TO QWidget::createWindowContainer
    QWidget *container = QWidget::createWindowContainer(view, this);
    
    //ADD SOURCE
    view->setSource(QUrl("your.qml"));
    
    //ADD THE CONTAINER TO YOUR LAYOUT.
    ui->verticalLayout->addWidget(container);