Qt for HarmonyOS/qtohosextras doc/Window Drag Resize Example - main.cpp
< Qt for HarmonyOS / qtohosextras doc
Window Drag Resize Example - main.cpp
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QApplication>
#include <QLabel>
#include <QLatin1String>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QtOhosExtras>
#include <functional>
namespace {
QWidget *createActionButton(QString label, QWidget *parent, std::function<void()> action)
{
auto *button = new QPushButton(label, parent);
QObject::connect(button, &QPushButton::clicked, parent, std::move(action));
return button;
}
QWidget *createDragResizeSettingToggleButton(QWidget *windowToEnableDrag, QWidget *parent)
{
auto *enableDragButton = new QPushButton(parent);
enableDragButton->setCheckable(true);
enableDragButton->setChecked(false);
auto updateDragResizable = [enableDragButton, windowToEnableDrag]() {
auto checked = enableDragButton->isChecked();
enableDragButton->setText(
checked
? QLatin1String("Reset window drag resizable")
: QLatin1String("Set window drag resizable"));
QtOhosExtras::setWindowDragResizable(windowToEnableDrag, checked);
};
updateDragResizable();
QObject::connect(
enableDragButton, &QPushButton::toggled, windowToEnableDrag, std::move(updateDragResizable));
return enableDragButton;
}
QWidget *createDragResizableWindow(const QString& windowName, QWidget *parent)
{
auto *window = new QWidget(parent);
window->setWindowFlags(Qt::Window);
window->setAttribute(Qt::WA_DeleteOnClose);
window->resize(800, 200);
auto *layout = new QVBoxLayout(window);
layout->addWidget(new QLabel(windowName, window));
layout->addWidget(
createDragResizeSettingToggleButton(window, window));
layout->addWidget(
createActionButton(
"Close window", window,
[window]() {
window->close();
}));
return window;
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle(QLatin1String("setWindowDragResizable example"));
auto *layout = new QVBoxLayout(&window);
layout->addWidget(
createActionButton(
"Create new sub window", &window,
[window = &window]() {
auto *newWindow = createDragResizableWindow("Sub window", window);
newWindow->show();
}));
layout->addWidget(
createActionButton(
"Create new float window", &window,
[window = &window]() {
auto *newWindow = createDragResizableWindow("Float window", window);
QtOhosExtras::setShowWindowAsFloatWindowHint(newWindow, true);
newWindow->show();
}));
window.show();
return app.exec();
}