diff --git a/examples/dockwidgets/MyFrameworkWidgetFactory.cpp b/examples/dockwidgets/MyFrameworkWidgetFactory.cpp index 01d63e8a..92716cc2 100644 --- a/examples/dockwidgets/MyFrameworkWidgetFactory.cpp +++ b/examples/dockwidgets/MyFrameworkWidgetFactory.cpp @@ -10,6 +10,7 @@ */ #include "MyFrameworkWidgetFactory.h" +#include "MyTitleBar_CSS.h" #include @@ -89,11 +90,13 @@ MySeparator::~MySeparator() = default; KDDockWidgets::TitleBar * CustomWidgetFactory::createTitleBar(KDDockWidgets::Frame *frame) const { + // Feel free to return MyTitleBar_CSS here instead, but just for education purposes! return new MyTitleBar(frame); } KDDockWidgets::TitleBar * CustomWidgetFactory::createTitleBar(KDDockWidgets::FloatingWindow *fw) const { + // Feel free to return MyTitleBar_CSS here instead, but just for education purposes! return new MyTitleBar(fw); } @@ -101,4 +104,3 @@ Layouting::Separator * CustomWidgetFactory::createSeparator(Layouting::Widget *p { return new MySeparator(parent); } - diff --git a/examples/dockwidgets/MyTitleBar_CSS.h b/examples/dockwidgets/MyTitleBar_CSS.h new file mode 100644 index 00000000..b38ad922 --- /dev/null +++ b/examples/dockwidgets/MyTitleBar_CSS.h @@ -0,0 +1,79 @@ +/* + This file is part of KDDockWidgets. + + SPDX-FileCopyrightText: 2019-2021 Klarälvdalens Datakonsult AB, a KDAB Group company + Author: Sérgio Martins + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only + + Contact KDAB at for commercial licensing options. +*/ + +#include + +/** + * @brief Shows how to implement a custom titlebar which uses "Qt StyleSheets". + * + * Derive from KDDockWidgets::DefaultWidgetFactory and override the two createTitleBar() methods. + * + * To try it out, modify examples/dockwidgets/MyFrameworkWidgetFactory.cpp to return a MyTitleBar_CSS instance. + * Run the example with: ./bin/kddockwidgets_example -p + * + * WARNINGS: + * - Qt StyleSheets are not recommended for new applications. Often you are able to style 90% of + * the application but then hit a road block. QStyle is much more powerful and flexible. + * - The Qt maintainers have manifested intention to deprecated stylesheets. + * - Stylesheets are supported for built-in QWidgets (QPushButton, QComboBox, etc.), any widget + * that's not in Qt needs to be crafted by the user, that includes, for example, having to paint + * your background manually. KDDockWidget::TitleBarWidget does this for your convenience though. + * - Qt stylesheets don't react to property changes (known old bug in Qt), for example: + * QLineEdit[readOnly="true"] { color: gray } + * this won't trigger when readOnly changes to false, you need to set/unset. This is QTBUG-51236 + * - KDDockWidgets::TitleBarWidget::isFocused is a property, there for needs to workaround the + * above bug by unsetting the sheet and setting it again. + */ +class MyTitleBar_CSS : public KDDockWidgets::TitleBarWidget +{ +public: + explicit MyTitleBar_CSS(KDDockWidgets::Frame *frame) + : KDDockWidgets::TitleBarWidget(frame) + { + init(); + } + + explicit MyTitleBar_CSS(KDDockWidgets::FloatingWindow *fw) + : KDDockWidgets::TitleBarWidget(fw) + { + init(); + } + + ~MyTitleBar_CSS() override; + + void initStyleSheet() + { + // Or use qApp->setStyleSheet(), as you prefer + setStyleSheet(QStringLiteral("KDDockWidgets--TitleBarWidget {" + "background: blue" + "}" + "KDDockWidgets--TitleBarWidget:hover {" + "background: red" + "}" + "KDDockWidgets--TitleBarWidget[isFocused=true] {" + "background: green" + "}")); + } + + void init() + { + initStyleSheet(); + connect(this, &KDDockWidgets::TitleBar::isFocusedChanged, this, [this] { + // Workaround QTBUG-51236, this makes the [isFocused=true] syntax useful + setStyleSheet(QString()); + initStyleSheet(); + }); + } +}; + +MyTitleBar_CSS::~MyTitleBar_CSS() +{ +}