Evgenii Legotckoi
Feb. 3, 2017, 10:55 p.m.

QML - Lesson 027. Adding QML in project based on QWidget

When developing your application to Qt may happen a situation where required in an application written in QWidgets implement functional written in QML. To solve such a problem can be used QQuickWidget class that is used to render QML .

Write a simple hello world, which will draw the code written in QML widget that is placed in the window, based on QWidget . To make it look as follows:


Configuration of Pro file

To use this object class, you need to connect a Pro project file quickwidgets module. Also add a resource file, which will contain qml files, namely QmlLabel.qml file.

QT       += core gui quickwidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qmlwidget
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

RESOURCES += \
    qml.qrc

widget.h

In the header file of the application window to include the header file of QQuickWidget and declare an object of this class.

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QQuickWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget* ui;
    QQuickWidget* m_quickWidget;
};

#endif // WIDGET_H

widget.cpp

The project was created with the help of interface designer, so the code will only be present in addition QQuickWidget object placement Grid Layout , which is responsible for the layout of the widgets in the window.

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_quickWidget = new QQuickWidget(this) ;
    m_quickWidget->setSource(QUrl("qrc:/QmlLabel.qml"));
    m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    ui->gridLayout->addWidget(m_quickWidget, 1, 0);
}

Widget::~Widget()
{
    delete ui;
}

QmlLabel.qml

Content of the Hello World on the QML

import QtQuick 2.5
import QtQuick.Controls 2.1

Item {

    Text {
        id: text
        text: qsTr("Hello world")

        anchors.centerIn: parent
    }

}

Download project

Recommended articles on this topic

By article asked0question(s)

0

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html
  • ИМ
    Oct. 5, 2024, 4:51 p.m.
    Приветствую Евгений! У меня вопрос. Можно ли вставлять свои классы в разметку редактора markdown? Допустим имея стандартную разметку: <ul> <li></li> <li></l…
  • d
    July 5, 2024, 8:02 p.m.
    Здравствуйте, возникает такая проблема (я новичок): ApplicationWindow неизвестный элемент. (М300) для TextField и Button аналогично. Могу предположить, что из-за более новой верси…