aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/nativestyle/items
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-05-29 11:33:23 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-06-02 09:16:20 +0000
commit09d35ba20553f27ba4183781ca9dc8dfa2e16f3f (patch)
tree10b292504e6863b864a5dbfaa72c1994ee233372 /src/imports/nativestyle/items
parentcc6c76a307025595f976e833f3dadbdc7de421e1 (diff)
Native style: improve type safety
A QuickStyleItem instantiated inside a QML file set property "control" to point to the control it styles. It's required that this item matches the control it styles, so that e.g a NativeStyle.Button is only used for styling a Button.qml (and not e.g a Slider.qml), since NativeStyle.Button will crash otherwise. We do a quick runtime check in debug mode rather than a compile time check to avoid to much churn in the c++ code to enforce this. Change-Id: I31dca31ab4f1fdf08780ffb01c533ac5837784f2 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/nativestyle/items')
-rw-r--r--src/imports/nativestyle/items/qquickstyleitem.cpp1
-rw-r--r--src/imports/nativestyle/items/qquickstyleitem.h12
2 files changed, 11 insertions, 2 deletions
diff --git a/src/imports/nativestyle/items/qquickstyleitem.cpp b/src/imports/nativestyle/items/qquickstyleitem.cpp
index bf6ac25e..a7bce506 100644
--- a/src/imports/nativestyle/items/qquickstyleitem.cpp
+++ b/src/imports/nativestyle/items/qquickstyleitem.cpp
@@ -36,7 +36,6 @@
#include "qquickstyleitem.h"
-#include <QtQml/qqmlinfo.h>
#include <QtQuick/qsgninepatchnode.h>
#include <QtQuick/private/qquickwindow_p.h>
diff --git a/src/imports/nativestyle/items/qquickstyleitem.h b/src/imports/nativestyle/items/qquickstyleitem.h
index 2535abc2..5d0b6671 100644
--- a/src/imports/nativestyle/items/qquickstyleitem.h
+++ b/src/imports/nativestyle/items/qquickstyleitem.h
@@ -39,6 +39,7 @@
#include <QtCore/qdebug.h>
#include <QtQml/qqml.h>
+#include <QtQml/qqmlinfo.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuickTemplates2/private/qquickcontrol_p.h>
@@ -209,7 +210,16 @@ protected:
inline QSize contentSize() { return m_contentSize.toSize(); }
inline static QStyle *style() { return QQuickNativeStyle::style(); }
- template <class T> inline const T* control() const { return static_cast<T *>(m_control.data()); }
+
+ template <class T> inline const T* control() const {
+#ifdef QT_DEBUG
+ if (!dynamic_cast<T *>(m_control.data())) {
+ qmlWarning(this) << "control property is not of correct type";
+ Q_UNREACHABLE();
+ }
+#endif
+ return static_cast<T *>(m_control.data());
+ }
#ifdef QT_DEBUG
bool m_debug = false;