0% found this document useful (0 votes)
23 views4 pages

Lab Exercise 7 (QML Widgets)

This document provides a tutorial on creating and using widgets in QML, covering the basics of labels, buttons, text fields and more. It includes 6 steps: 1) Creating a basic QML application, 2) Adding a label widget, 3) Running the application, 4) Adding a button widget, 5) Adding a text field widget, and 6) Running the final application. The goal is to demonstrate how to create, position and interact with different types of widgets in QML.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Lab Exercise 7 (QML Widgets)

This document provides a tutorial on creating and using widgets in QML, covering the basics of labels, buttons, text fields and more. It includes 6 steps: 1) Creating a basic QML application, 2) Adding a label widget, 3) Running the application, 4) Adding a button widget, 5) Adding a text field widget, and 6) Running the final application. The goal is to demonstrate how to create, position and interact with different types of widgets in QML.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Exercise 7 – QML Widgets

Creating widgets in QML is an essential part of building user interfaces in Qt Quick


applications. Widgets in QML are components or elements that provide user
interface elements like buttons, text inputs, labels, and more. In this tutorial, we'll
cover the basics of creating and using widgets in QML.
Prerequisites:
Before you start, ensure you have Qt and Qt Creator installed. You can download Qt
from the official website (https://www.qt.io/download).
Step 1: Creating a Basic QML Application
Open Qt Creator and create a new Qt Quick Application project.
In your project directory, locate the main.qml file. This is where we'll create our QML
widgets.
Step 2: Creating a Label Widget
Let's start by creating a simple label widget.
import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
visible: true
width: 400
height: 200
title: "Widget Example"

Label {
text: "Hello, QML Widgets!"
anchors.centerIn: parent
}
}
Here, we import the QtQuick.Controls module for creating widgets. We've added a
Label widget with the text "Hello, QML Widgets!" centered within the window.
Step 3: Running the Application
Build and run your application. You should see a window displaying the label.
Step 4: Creating a Button Widget
Let's add a button widget that displays a message when clicked.
import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
visible: true
width: 400
height: 200
title: "Widget Example"

Label {
text: "Hello, QML Widgets!"
anchors.centerIn: parent
}

Button {
text: "Click Me"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: label.bottom
onClicked: {
label.text = "Button Clicked!"
}
}
}
We added a Button widget with the text "Click Me" and a Label with the initial text.
When the button is clicked, it changes the label's text.
Step 5: Creating an Input Field (TextField) Widget
Next, let's add an input field where users can enter text.
import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
visible: true
width: 400
height: 200
title: "Widget Example"

Label {
text: "Hello, QML Widgets!"
anchors.centerIn: parent
}

Button {
text: "Click Me"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: label.bottom
onClicked: {
label.text = "Button Clicked!"
}
}

TextField {
placeholderText: "Enter text"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: button.bottom
onTextChanged: {
label.text = "You entered: " + text
}
}
}
We've added a TextField widget with a placeholder text "Enter text." When the text in
the input field changes, it updates the label with "You entered: " followed by the
entered text.
Step 6: Running the Final Application
Build and run your application again. You should now see a label, a button, and an
input field.
This tutorial covers the basics of creating and using widgets in QML. You can explore
more widgets and their properties in the Qt documentation
(https://doc.qt.io/qt-5/qmltypes.html).

You might also like