Learn QT-WEBASSEMBLY with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Qt WebAssembly Counter Example
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Qt WebAssembly Counter");
int count = 0;
QLabel *label = new QLabel(QString("Counter: %1").arg(count));
QPushButton *btnIncrement = new QPushButton("+");
QPushButton *btnDecrement = new QPushButton("-");
QPushButton *btnReset = new QPushButton("Reset");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
layout->addWidget(btnIncrement);
layout->addWidget(btnDecrement);
layout->addWidget(btnReset);
window.setLayout(layout);
QObject::connect(btnIncrement, &QPushButton::clicked, [&](){
count++;
label->setText(QString("Counter: %1").arg(count));
});
QObject::connect(btnDecrement, &QPushButton::clicked, [&](){
count--;
label->setText(QString("Counter: %1").arg(count));
});
QObject::connect(btnReset, &QPushButton::clicked, [&](){
count = 0;
label->setText(QString("Counter: %1").arg(count));
});
window.show();
return app.exec();
}
Demonstrates a simple counter layout using Qt Widgets and C++ compiled to WebAssembly for browser interactivity.
2
Qt WebAssembly Hello World
#include <QApplication>
#include <QWidget>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Hello Qt WebAssembly");
QLabel *label = new QLabel("Hello, WebAssembly!");
label->setAlignment(Qt::AlignCenter);
window.setFixedSize(300, 200);
label->setParent(&window);
window.show();
return app.exec();
}
Displays a simple 'Hello World' label in a Qt WebAssembly browser app.
3
Qt WebAssembly Button Click Example
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Button Click Example");
QLabel *label = new QLabel("Button not clicked");
QPushButton *button = new QPushButton("Click Me");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
layout->addWidget(button);
window.setLayout(layout);
QObject::connect(button, &QPushButton::clicked, [&](){
label->setText("Button Clicked!");
});
window.show();
return app.exec();
}
A simple button click event example for Qt WebAssembly.
4
Qt WebAssembly Text Input Example
#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QVBoxLayout>
#include <QString>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Text Input Example");
QLineEdit *lineEdit = new QLineEdit();
QLabel *label = new QLabel("Enter text above");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit);
layout->addWidget(label);
window.setLayout(layout);
QObject::connect(lineEdit, &QLineEdit::textChanged, [&](const QString &text){
label->setText("You typed: " + text);
});
window.show();
return app.exec();
}
Captures user input from a text box and displays it.
5
Qt WebAssembly Slider Example
#include <QApplication>
#include <QWidget>
#include <QSlider>
#include <QLabel>
#include <QVBoxLayout>
#include <Qt>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Slider Example");
QLabel *label = new QLabel("Value: 0");
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 100);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
layout->addWidget(slider);
window.setLayout(layout);
QObject::connect(slider, &QSlider::valueChanged, [&](int value){
label->setText("Value: " + QString::number(value));
});
window.show();
return app.exec();
}
Demonstrates a slider controlling a label value.
6
Qt WebAssembly Toggle Button Example
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QString>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Toggle Button Example");
QPushButton *button = new QPushButton("OFF");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(button);
window.setLayout(layout);
QObject::connect(button, &QPushButton::clicked, [=]() mutable {
if(button->text() == "OFF") button->setText("ON");
else button->setText("OFF");
});
window.show();
return app.exec();
}
A toggle button changes the text between ON and OFF.
7
Qt WebAssembly Color Change Example
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QPalette>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Color Change Example");
QLabel *label = new QLabel("Watch my color");
QPushButton *button = new QPushButton("Change Color");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(label);
layout->addWidget(button);
window.setLayout(layout);
QObject::connect(button, &QPushButton::clicked, [=]() {
QPalette pal = label->palette();
pal.setColor(QPalette::WindowText, Qt::red);
label->setPalette(pal);
});
window.show();
return app.exec();
}
Changes label color on button click.
8
Qt WebAssembly Checkbox Example
#include <QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Checkbox Example");
QCheckBox *checkBox = new QCheckBox("Check me");
QLabel *label = new QLabel("Unchecked");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(checkBox);
layout->addWidget(label);
window.setLayout(layout);
QObject::connect(checkBox, &QCheckBox::stateChanged, [&](int state){
label->setText(state == Qt::Checked ? "Checked" : "Unchecked");
});
window.show();
return app.exec();
}
A checkbox toggles a label between checked and unchecked.
9
Qt WebAssembly Progress Bar Example
#include <QApplication>
#include <QWidget>
#include <QSlider>
#include <QProgressBar>
#include <QVBoxLayout>
#include <Qt>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("Progress Bar Example");
QSlider *slider = new QSlider(Qt::Horizontal);
QProgressBar *progress = new QProgressBar();
slider->setRange(0, 100);
progress->setRange(0, 100);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(slider);
layout->addWidget(progress);
window.setLayout(layout);
QObject::connect(slider, &QSlider::valueChanged, [&](int value){
progress->setValue(value);
});
window.show();
return app.exec();
}
Updates a progress bar based on a slider value.