Why so many QStyles

David Faure faure at kde.org
Sun Jan 4 12:51:15 UTC 2015


On Thursday 02 October 2014 22:10:05 Hugo Pereira Da Costa wrote:
> In Qt5 this seems not to be true anymore and I've seen up to 6 QStyle 
> running concurently for a single Kate Window, increasing to 13 when 
> opening 'open file' dialog, decreasing back to 6 when closing it, etc.
> Is this a known feature ? Will this stay ?

I'm surprised that nobody has thought of running the app in gdb and putting a 
breakpoint on the QStyle constructor, to answer these questions with actual 
facts.

So here I come.

gdb kate
start
b 'QStyle::QStyle'
c

1) first QStyle is created by the QApp initialization.
2) second QStyle is KLineEditStyle, a proxy style used by KLineEdit
But I suppose your question is about OxygenStyle instances, not other styles, 
right?

Ah, but then....
3) the first time the lineedit calls style(), qproxystyle.cpp does

100│     if (!baseStyle) // Use application desktop style
101├>        baseStyle = 
QStyleFactory::create(QApplicationPrivate::desktopStyleKey());

So it creates another instance of the main widget style (e.g. oxygen or 
breeze), as the base style for the proxy.

BTW the code in Qt4 is the same, it's just that KLineEdit wasn't using 
QProxyStyle but rather KdeUiProxyStyle.

This patch for kcompletion.git works around the issue by setting the base 
style for the proxystyle to be the existing app style, rather than creating a 
new one.

diff --git a/src/klineedit_p.h b/src/klineedit_p.h
index 0be32ba..cf63e09 100644
--- a/src/klineedit_p.h
+++ b/src/klineedit_p.h
@@ -29,6 +29,7 @@
 #include <QPropertyAnimation>
 #include <QIcon>
 #include <QProxyStyle>
+#include <QApplication>
 
 class KCompletionBox;
 class LineEditUrlDropEventFilter;
@@ -186,7 +187,7 @@ class KLineEditStyle : public QProxyStyle
     Q_OBJECT
 public:
     KLineEditStyle(QStyle *style)
-        : QProxyStyle(),
+        : QProxyStyle(qApp->style()),
           m_overlap(0),
           m_subStyle(style),
           m_sentinel(false)


This leads me to the following thoughts:

1) I'm not sure why QProxyStyle defaults to creating another instance of the 
app style rather than using the existing one. Maybe J-P Nurmi knows? Cc'ed.

2) I can't think of a reason against passing qApp->style() to the 
KLineEditStyle ctor; the rest of the code in QProxyStyle::ensureBaseStyle is 
about the -style cmdline override (which qApp->style() honours too).

3) In any case, Qt creates multiple instances of widget styles, AFAICS, in 
many places. So yes, your style should be ready for that.

-- 
David Faure, faure at kde.org, http://www.davidfaure.fr
Working on KDE Frameworks 5



More information about the Kde-frameworks-devel mailing list