misc: minor tweaks

1. Finish the simple title bar of the QOpenGLWidget demo
2. Minor improvements of the Quick implementation

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-04-22 12:04:38 +08:00
parent 130d174dc2
commit 3f17626c64
8 changed files with 109 additions and 13 deletions

View File

@ -25,6 +25,12 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "glwidget.h" #include "glwidget.h"
#include <QtWidgets/qboxlayout.h> #include <QtWidgets/qboxlayout.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qpushbutton.h>
FRAMELESSHELPER_USE_NAMESPACE
using namespace Global;
MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent) MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent)
{ {
@ -33,19 +39,49 @@ MainWindow::MainWindow(QWidget *parent) : FramelessWidget(parent)
MainWindow::~MainWindow() = default; MainWindow::~MainWindow() = default;
void MainWindow::updateMaximizeButton()
{
m_maxBtn->setText(isZoomed() ? tr("RESTORE") : tr("MAXIMIZE"));
}
void MainWindow::setupUi() void MainWindow::setupUi()
{ {
resize(800, 600); m_titleLabel.reset(new QLabel(this));
setWindowTitle(tr("QOpenGLWidget demo")); QFont f = font();
m_titleBar.reset(new QWidget(this)); f.setPointSize(kDefaultTitleBarFontPointSize);
m_titleBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); m_titleLabel->setFont(f);
m_titleBar->setFixedHeight(30); connect(this, &MainWindow::windowTitleChanged, m_titleLabel.data(), &QLabel::setText);
m_minBtn.reset(new QPushButton(this));
m_minBtn->setText(tr("MINIMIZE"));
connect(m_minBtn.data(), &QPushButton::clicked, this, &MainWindow::showMinimized);
m_maxBtn.reset(new QPushButton(this));
updateMaximizeButton();
connect(m_maxBtn.data(), &QPushButton::clicked, this, &MainWindow::toggleMaximized);
connect(this, &MainWindow::zoomedChanged, this, &MainWindow::updateMaximizeButton);
m_closeBtn.reset(new QPushButton(this));
m_closeBtn->setText(tr("CLOSE"));
connect(m_closeBtn.data(), &QPushButton::clicked, this, &MainWindow::close);
m_titleBarWidget.reset(new QWidget(this));
m_titleBarWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_titleBarWidget->setFixedHeight(kDefaultTitleBarHeight);
const auto titleBarLayout = new QHBoxLayout(m_titleBarWidget.data());
titleBarLayout->setSpacing(0);
titleBarLayout->setContentsMargins(0, 0, 0, 0);
titleBarLayout->addSpacerItem(new QSpacerItem(kDefaultTitleBarTitleLabelMargin, kDefaultTitleBarTitleLabelMargin));
titleBarLayout->addWidget(m_titleLabel.data());
titleBarLayout->addStretch();
titleBarLayout->addWidget(m_minBtn.data());
titleBarLayout->addWidget(m_maxBtn.data());
titleBarLayout->addWidget(m_closeBtn.data());
m_titleBarWidget->setLayout(titleBarLayout);
m_glWidget.reset(new GLWidget(this)); m_glWidget.reset(new GLWidget(this));
const auto mainLayout = new QVBoxLayout(this); const auto mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(0); mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(m_titleBar.data()); mainLayout->addWidget(m_titleBarWidget.data());
mainLayout->addWidget(m_glWidget.data()); mainLayout->addWidget(m_glWidget.data());
setLayout(mainLayout); setLayout(mainLayout);
setTitleBarWidget(m_titleBar.data()); setTitleBarWidget(m_titleBarWidget.data());
resize(800, 600);
setWindowTitle(tr("QOpenGLWidget demo"));
} }

View File

@ -26,6 +26,11 @@
#include <framelesswidget.h> #include <framelesswidget.h>
QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
QT_END_NAMESPACE
class GLWidget; class GLWidget;
class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessWidget) class MainWindow : public FRAMELESSHELPER_PREPEND_NAMESPACE(FramelessWidget)
@ -37,10 +42,17 @@ public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override; ~MainWindow() override;
public Q_SLOTS:
void updateMaximizeButton();
private: private:
void setupUi(); void setupUi();
private: private:
QScopedPointer<QWidget> m_titleBar; QScopedPointer<QLabel> m_titleLabel;
QScopedPointer<QPushButton> m_minBtn;
QScopedPointer<QPushButton> m_maxBtn;
QScopedPointer<QPushButton> m_closeBtn;
QScopedPointer<QWidget> m_titleBarWidget;
QScopedPointer<GLWidget> m_glWidget; QScopedPointer<GLWidget> m_glWidget;
}; };

View File

@ -26,7 +26,7 @@
#include <framelesshelpercore_global.h> #include <framelesshelpercore_global.h>
#if __has_include(<QtQml/qqmlregistration.h>) #if __has_include(<QtQml/qqmlregistration.h>)
#include <QtQml/qqmlregistration.h> # include <QtQml/qqmlregistration.h>
#endif #endif
#ifndef FRAMELESSHELPER_QUICK_API #ifndef FRAMELESSHELPER_QUICK_API

View File

@ -25,6 +25,9 @@
#pragma once #pragma once
#include "framelesshelperquick_global.h" #include "framelesshelperquick_global.h"
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
# include <QtQml/qqmlextensionplugin.h>
#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QQmlEngine; class QQmlEngine;
@ -35,6 +38,20 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
namespace FramelessHelper::Quick namespace FramelessHelper::Quick
{ {
FRAMELESSHELPER_QUICK_API void registerTypes(QQmlEngine *engine); FRAMELESSHELPER_QUICK_API void registerTypes(QQmlEngine *engine);
} } // namespace FramelessHelper::Quick
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
class FRAMELESSHELPER_QUICK_API FramelessHelperQuickPlugin : public QQmlEngineExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
public:
explicit FramelessHelperQuickPlugin(QObject *parent = nullptr);
~FramelessHelperQuickPlugin() override;
void initializeEngine(QQmlEngine *engine, const char *uri) override;
};
#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -1,4 +1,7 @@
<RCC> <RCC>
<qresource prefix="/org/wangwenx190/FramelessHelper">
<file alias="qmldir">module/qmldir</file>
</qresource>
<qresource prefix="/org.wangwenx190.FramelessHelper"> <qresource prefix="/org.wangwenx190.FramelessHelper">
<file alias="images/dark/chrome-close.svg">../core/images/dark/chrome-close.svg</file> <file alias="images/dark/chrome-close.svg">../core/images/dark/chrome-close.svg</file>
<file alias="images/dark/chrome-maximize.svg">../core/images/dark/chrome-maximize.svg</file> <file alias="images/dark/chrome-maximize.svg">../core/images/dark/chrome-maximize.svg</file>

View File

@ -93,4 +93,26 @@ void FramelessHelper::Quick::registerTypes(QQmlEngine *engine)
qmlRegisterModule(QUICK_URI_FULL); qmlRegisterModule(QUICK_URI_FULL);
} }
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
FramelessHelperQuickPlugin::FramelessHelperQuickPlugin(QObject *parent) : QQmlEngineExtensionPlugin(parent)
{
}
FramelessHelperQuickPlugin::~FramelessHelperQuickPlugin() = default;
void FramelessHelperQuickPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
Q_ASSERT(engine);
Q_ASSERT(uri);
if (!engine || !uri) {
return;
}
Q_ASSERT(QLatin1String(uri) == QLatin1String(FRAMELESSHELPER_QUICK_URI));
if (QLatin1String(uri) != QLatin1String(FRAMELESSHELPER_QUICK_URI)) {
return;
}
FramelessHelper::Quick::registerTypes(engine);
}
#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

6
src/quick/module/qmldir Normal file
View File

@ -0,0 +1,6 @@
module org.wangwenx190.FramelessHelper
linktarget FramelessHelperQuick
plugin FramelessHelperQuick
classname FramelessHelperQuickPlugin
typeinfo FramelessHelperQuick.qmltypes
prefer :/org/wangwenx190/FramelessHelper/