summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/widgets/doc/images/stylepluginexample.pngbin5259 -> 15331 bytes
-rw-r--r--examples/widgets/doc/src/styleplugin.qdoc41
-rw-r--r--examples/widgets/tools/styleplugin/plugin/CMakeLists.txt24
-rw-r--r--examples/widgets/tools/styleplugin/plugin/plugin.pro12
-rw-r--r--examples/widgets/tools/styleplugin/plugin/simplestyle.cpp2
-rw-r--r--examples/widgets/tools/styleplugin/stylewindow/main.cpp9
-rw-r--r--examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp8
7 files changed, 77 insertions, 19 deletions
diff --git a/examples/widgets/doc/images/stylepluginexample.png b/examples/widgets/doc/images/stylepluginexample.png
index 05d8c6b5cf..9ff69c512b 100644
--- a/examples/widgets/doc/images/stylepluginexample.png
+++ b/examples/widgets/doc/images/stylepluginexample.png
Binary files differ
diff --git a/examples/widgets/doc/src/styleplugin.qdoc b/examples/widgets/doc/src/styleplugin.qdoc
index c3c4c8d223..cb0fbe15b8 100644
--- a/examples/widgets/doc/src/styleplugin.qdoc
+++ b/examples/widgets/doc/src/styleplugin.qdoc
@@ -25,20 +25,23 @@
class. Style plugins are loaded by Qt and made available through
QStyleFactory; we will look at this later. We have implemented \c
SimpleStylePlugin, which provides \c SimpleStyle. The new style
- contributes to widget styling by drawing button backgrounds in
- red - not a major contribution, but it still makes a new style.
+ contributes to widget styling by changing the text color of the
+ text edit widget to red - not a major contribution, but it still
+ makes a new style.
The new style is platform agnostic in the sense that it is not
based on any specific style implementation, but uses QProxyStyle
to merely tweak the looks in the current application style that
defaults to the native system style.
- \note On some platforms, the native style will prevent the button
- from having a red background. In this case, try to run the example
- in another style (e.g., fusion).
+ \note On some platforms, the native style may overwrite some custom
+ stylings, e.g., button background color. In that case, try to run
+ your application in another style (e.g., fusion). You may do this
+ by passing \c{-style fusion} as a command line argument to your
+ application.
We test the plugin with \c StyleWindow, in which we display a
- QPushButton. The \c SimpleStyle and \c StyleWindow classes do not
+ QTextEdit. The \c SimpleStyle and \c StyleWindow classes do not
contain any plugin specific functionality and their implementations
are trivial; we will therefore leap past them and head on to the \c
SimpleStylePlugin and the \c main() function. After we have looked
@@ -96,8 +99,30 @@
In the plugin profile we need to set the lib template as we are
building a shared library instead of an executable. We must also
set the config to plugin. We set the library to be stored in the
- styles folder under stylewindow because this is a path in which Qt
- will search for style plugins.
+ \c{styles} folder next to the main executable because this is a path
+ in which Qt will search for style plugins.
+
+ \section2 Using CMake to Set up the Simple Style Plugin
+
+ When using CMake, we use \l{qt6_add_plugin}{qt_add_plugin}
+ to create the \c simplestyleplugin plugin:
+
+ \snippet tools/styleplugin/plugin/CMakeLists.txt 0
+
+ On Windows and Linux, we place the plugin into the \c{styles} folder
+ next to the main executable, i.e., \c{styleplugin.exe}:
+
+ \snippet tools/styleplugin/plugin/CMakeLists.txt 2
+
+ And on macOS, we store the \c simplestyleplugin into the
+ \c{Contents/PlugIns/styles} folder of the App Bundle.
+
+ \snippet tools/styleplugin/plugin/CMakeLists.txt 1
+
+ \note On macOS, when creating an App Bundle, store the plugins in
+ the \c PlugIns folder and not next to the main executable in
+ the \c MacOS folder as the latter will cause issues during signing
+ and distribution of the app.
\section1 Related Articles and Examples
diff --git a/examples/widgets/tools/styleplugin/plugin/CMakeLists.txt b/examples/widgets/tools/styleplugin/plugin/CMakeLists.txt
index 41df10c578..b82d3ab065 100644
--- a/examples/widgets/tools/styleplugin/plugin/CMakeLists.txt
+++ b/examples/widgets/tools/styleplugin/plugin/CMakeLists.txt
@@ -1,15 +1,33 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
+#! [0]
qt_add_plugin(simplestyleplugin
CLASS_NAME SimpleStylePlugin
simplestyle.cpp simplestyle.h
simplestyleplugin.cpp simplestyleplugin.h
)
+#! [0]
-set_target_properties(simplestyleplugin PROPERTIES
- LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/stylewindow/styles"
-)
+if(QT_FEATURE_debug AND APPLE)
+ set_property(simplestyleplugin
+ APPEND_STRING PROPERTY OUTPUT_NAME "_debug")
+endif()
+
+get_target_property(is_bundle styleplugin MACOSX_BUNDLE)
+if(APPLE AND is_bundle)
+#! [1]
+ set_target_properties(simplestyleplugin PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY "$<TARGET_BUNDLE_CONTENT_DIR:styleplugin>/PlugIns/styles"
+ )
+#! [1]
+else()
+#! [2]
+ set_target_properties(simplestyleplugin PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY "$<TARGET_FILE_DIR:styleplugin>/styles"
+ )
+#! [2]
+endif()
target_link_libraries(simplestyleplugin PRIVATE
Qt6::Core
diff --git a/examples/widgets/tools/styleplugin/plugin/plugin.pro b/examples/widgets/tools/styleplugin/plugin/plugin.pro
index 35184fc82a..5ab00016ca 100644
--- a/examples/widgets/tools/styleplugin/plugin/plugin.pro
+++ b/examples/widgets/tools/styleplugin/plugin/plugin.pro
@@ -12,7 +12,17 @@ win32 {
CONFIG(debug, release|debug):DESTDIR = ../debug/styles/
CONFIG(release, release|debug):DESTDIR = ../release/styles/
} else {
- DESTDIR = ../styles/
+ macos {
+ # The non-app-bundle case is not supported with qmake, because
+ # the plugin project cannot know whether the app is built
+ # as a bundle or not.
+ DESTDIR = ../styleplugin.app/Contents/PlugIns/styles/
+ contains(QT_CONFIG, debug) {
+ TARGET = $$join(TARGET,,,_debug)
+ }
+ } else {
+ DESTDIR = ../styles/
+ }
}
EXAMPLE_FILES += simplestyle.json
diff --git a/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp b/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp
index 93bea72bcd..fa60d50140 100644
--- a/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp
+++ b/examples/widgets/tools/styleplugin/plugin/simplestyle.cpp
@@ -5,5 +5,5 @@
void SimpleStyle::polish(QPalette &palette)
{
- palette.setBrush(QPalette::Button, Qt::red);
+ palette.setBrush(QPalette::Text, Qt::red);
}
diff --git a/examples/widgets/tools/styleplugin/stylewindow/main.cpp b/examples/widgets/tools/styleplugin/stylewindow/main.cpp
index 0b4ca6320f..0b0a295410 100644
--- a/examples/widgets/tools/styleplugin/stylewindow/main.cpp
+++ b/examples/widgets/tools/styleplugin/stylewindow/main.cpp
@@ -10,10 +10,15 @@
int main(int argv, char *args[])
{
QApplication app(argv, args);
- QApplication::setStyle(QStyleFactory::create("simplestyle"));
+
+ QStyle *style = QStyleFactory::create("simplestyle");
+ if (!style)
+ qFatal("Cannot load the 'simplestyle' plugin.");
+
+ QApplication::setStyle(style);
StyleWindow window;
- window.resize(200, 50);
+ window.resize(350, 50);
window.show();
return app.exec();
diff --git a/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp
index fd449057e6..9268666c21 100644
--- a/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp
+++ b/examples/widgets/tools/styleplugin/stylewindow/stylewindow.cpp
@@ -3,18 +3,18 @@
#include <QGridLayout>
#include <QGroupBox>
-#include <QPushButton>
+#include <QTextEdit>
#include "stylewindow.h"
StyleWindow::StyleWindow()
{
- QPushButton *styledButton = new QPushButton(tr("Big Red Button"));
+ QTextEdit *styledTextEdit = new QTextEdit(tr("The quick brown fox jumps over the lazy dog"));
QGridLayout *layout = new QGridLayout;
- layout->addWidget(styledButton);
+ layout->addWidget(styledTextEdit);
- QGroupBox *styleBox = new QGroupBox(tr("A simple style button"));
+ QGroupBox *styleBox = new QGroupBox(tr("A simple styled text edit"));
styleBox->setLayout(layout);
QGridLayout *outerLayout = new QGridLayout;