Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, July 2007
QStyle and StyleSheets


Message 1 in thread

Hello,

I am developing a custom QStyle, and I would like to load a global app
stylesheet within the QStyle plugin. The problem is, when starting the app
like "myapp -style mystyle", I cannot use app->setStyleSheet() in the QStyle
constructor since the QApplication object is not fully created yet (my guess
-- this segfaults).

Is there an elegent solution to apply the stylesheet just once within the
QStyle object?

Regards,

P-E

Message 2 in thread

in the constructor:
QTimer::singleShot(0,myStyle,SLOT(applySheet()));
applySheet() will be called as soon as the app is up and the eventchain 
running.

Thomas

Am Freitag, 13. Juli 2007 16:43 schrieb Pierre-Étienne Messier:
> Hello,
>
> I am developing a custom QStyle, and I would like to load a global app
> stylesheet within the QStyle plugin. The problem is, when starting the app
> like "myapp -style mystyle", I cannot use app->setStyleSheet() in the
> QStyle constructor since the QApplication object is not fully created yet
> (my guess -- this segfaults).
>
> Is there an elegent solution to apply the stylesheet just once within the
> QStyle object?
>
> Regards,
>
> P-E

-- 
 [ signature omitted ] 

Message 3 in thread

Hello,

@Thomas: Oops, that was an accident! Using 'reply' from Gmail did send the
reply only to you.. my bad! Brigning the thread back on the list...

Now on the topic..

I'm currently trying to apply a specific style in the polish() event of a
widget, but this leads to a segfault too. The code (compiled as a plugin)
(cleaned a bit):

mystyle.h:
----------------------------------------------------------------
#ifndef MYSTYLE_H_
#define MYSTYLE_H_

#include <QMotifStyle>
#include <QWidget>


class MyStyle: public QMotifStyle
{
    Q_OBJECT

public:
    MyStyle();
    virtual ~MyStyle();

    void polish(QWidget* w);
};

#endif /*MYSTYLE_H_*/
----------------------------------------------------------------



mystyle.cpp:
----------------------------------------------------------------

#include "mystyle.h"

MyStyle::MyStyle():
    QMotifStyle(false)
{
}

MyStyle::~MyStyle()
{
}

void MyStyle::polish(QWidget* w)
{
    w->setStyleSheet("QPushButton { color: red }");
}
----------------------------------------------------------------


For some reason though, using :
    w->setStyleSheet("");

Works (as in "no segfault")...

P-E


On 7/20/07, Thomas Lübking <thomas.luebking@xxxxxx> wrote:
>
> You could try to check qApp()->style() before applying the sheet to the
> app
> (thus no style sheeding for apps with substyles, i.e. designer), or avoid
> appyling the sheet for apps (in the constructor) at all and apply sheets
> to
> toplevel widgets only during polishing.
> Chances i can see the sources anywhere?
>
> Thomas
>
> ps:
> did you take this off from qt-interest by intention or by accident?
>
> Am Freitag, 20. Juli 2007 21:23 schrieben Sie:
> > Works really great! However, if I load the style plugin within Designer,
> > this crashes the app (well, this code tries to apply the stylesheet to
> > Designer, not to the form)... Is there an alternative?
> >
> > Regards,
> >
> > P-E
> >
> > On 7/13/07, Thomas Lübking <thomas.luebking@xxxxxx> wrote:
> > > in the constructor:
> > > QTimer::singleShot(0,myStyle,SLOT(applySheet()));
> > > applySheet() will be called as soon as the app is up and the
> eventchain
> > > running.
> > >
> > > Thomas
>



-- 
 [ signature omitted ] 

Message 4 in thread

as qt requires (for whatever reason) a (const) reference  ("&") to a  qstring 
as sheet, try

MyStyle::MyStyle():
    QMotifStyle(false)
{
}

MyStyle::~MyStyle()
{
}

static const QString mySheet = "QPushButton { color: red }";
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

void MyStyle::polish(QWidget* w)
{
    w->setStyleSheet(mySheet);
//                          ^^^^^^^^^
}

(i'd bet the "" string is catched by kinda "if (sheet.isEmpty()) return;" 
statement)

Thomas

Am Montag, 23. Juli 2007 15:48 schrieb Pierre-Étienne Messier:
> Hello,
>
> @Thomas: Oops, that was an accident! Using 'reply' from Gmail did send the
> reply only to you.. my bad! Brigning the thread back on the list...
>
> Now on the topic..
>
> I'm currently trying to apply a specific style in the polish() event of a
> widget, but this leads to a segfault too. The code (compiled as a plugin)
> (cleaned a bit):
>
> mystyle.h:
> ----------------------------------------------------------------
> #ifndef MYSTYLE_H_
> #define MYSTYLE_H_
>
> #include <QMotifStyle>
> #include <QWidget>
>
>
> class MyStyle: public QMotifStyle
> {
>     Q_OBJECT
>
> public:
>     MyStyle();
>     virtual ~MyStyle();
>
>     void polish(QWidget* w);
> };
>
> #endif /*MYSTYLE_H_*/
> ----------------------------------------------------------------
>
>
>
> mystyle.cpp:
> ----------------------------------------------------------------
>
> #include "mystyle.h"
>
> MyStyle::MyStyle():
>     QMotifStyle(false)
> {
> }
>
> MyStyle::~MyStyle()
> {
> }
>
> void MyStyle::polish(QWidget* w)
> {
>     w->setStyleSheet("QPushButton { color: red }");
> }
> ----------------------------------------------------------------
>
>
> For some reason though, using :
>     w->setStyleSheet("");
>
> Works (as in "no segfault")...
>
> P-E
>
> On 7/20/07, Thomas Lübking <thomas.luebking@xxxxxx> wrote:
> > You could try to check qApp()->style() before applying the sheet to the
> > app
> > (thus no style sheeding for apps with substyles, i.e. designer), or avoid
> > appyling the sheet for apps (in the constructor) at all and apply sheets
> > to
> > toplevel widgets only during polishing.
> > Chances i can see the sources anywhere?
> >
> > Thomas
> >
> > ps:
> > did you take this off from qt-interest by intention or by accident?
> >
> > Am Freitag, 20. Juli 2007 21:23 schrieben Sie:
> > > Works really great! However, if I load the style plugin within
> > > Designer, this crashes the app (well, this code tries to apply the
> > > stylesheet to Designer, not to the form)... Is there an alternative?
> > >
> > > Regards,
> > >
> > > P-E
> > >
> > > On 7/13/07, Thomas Lübking <thomas.luebking@xxxxxx> wrote:
> > > > in the constructor:
> > > > QTimer::singleShot(0,myStyle,SLOT(applySheet()));
> > > > applySheet() will be called as soon as the app is up and the
> >
> > eventchain
> >
> > > > running.
> > > >
> > > > Thomas

-- 
 [ signature omitted ] 

Message 5 in thread

On 23.07.07 19:24:47, Thomas Lübking wrote:
> as qt requires (for whatever reason) a (const) reference  ("&") to a  qstring 
> as sheet, try

Uhm, I think you need to revisit your C++ book, when you do

foo("lalala");

where foo is declared as 

void foo(const QString&);

then behind your back a QString instance is created (through the const
char* constructor) and this QString instance is valid as long as the
function executes. The only way to create a segfault with this is if foo
saves the reference to that QString instance.

The only things that setStyleSheet does with the given string is
assigning its value to another QString and executing a function on it
(isEmpty()).

> (i'd bet the "" string is catched by kinda "if (sheet.isEmpty()) return;" 
> statement)

Not quite, if the stylesheet string is empty the original style is
recreated.

Andreas

-- 
 [ signature omitted ] 

Message 6 in thread

Indeed, the problem does not come from setStyleSheet but from the polish()
function. It seems (from the gdb backtrack) that calling setStyleSheet
recalls polish, thus creating an infinite loop:

#3391 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950, w=0x8b1d530)
at styles/qstylesheetstyle.cpp:2180
#3392 0x00ea6dbc in QWidget::setStyleSheet (this=0x8b1d530,
styleSheet=@0xac2bec) at kernel/qwidget.cpp:1873
#3393 0x00abd13e in MyStyle::polish () from
/home/dev/qt_plugins/styles/libmystyleplugin.so
#3394 0x010cc9f7 in QStyleSheetStyle::polish (this=0x8b11950, w=0x8b1d530)
at styles/qstylesheetstyle.cpp:2102
#3395 0x010c75f0 in updateWidgets (widgets=@0xbf4675e0) at
styles/qstylesheetstyle.cpp:2041
#3396 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950, w=0x8b1d530)
at styles/qstylesheetstyle.cpp:2180
#3397 0x00ea6dbc in QWidget::setStyleSheet (this=0x8b1d530,
styleSheet=@0xac2bec) at kernel/qwidget.cpp:1873
#3398 0x00abd13e in MyStyle::polish () from
/home/dev/qt_plugins/styles/libmystyleplugin.so
#3399 0x010cc9f7 in QStyleSheetStyle::polish (this=0x8b11950, w=0x8b1d530)
at styles/qstylesheetstyle.cpp:2102
#3400 0x010c75f0 in updateWidgets (widgets=@0xbf467850) at
styles/qstylesheetstyle.cpp:2041
#3401 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950, w=0x8b1d530)
at styles/qstylesheetstyle.cpp:2180

P-E

On 7/23/07, Andreas Pakulat <apaku@xxxxxx> wrote:
>
> On 23.07.07 19:24:47, Thomas Lübking wrote:
> > as qt requires (for whatever reason) a (const) reference  ("&") to
> a  qstring
> > as sheet, try
>
> Uhm, I think you need to revisit your C++ book, when you do
>
> foo("lalala");
>
> where foo is declared as
>
> void foo(const QString&);
>
> then behind your back a QString instance is created (through the const
> char* constructor) and this QString instance is valid as long as the
> function executes. The only way to create a segfault with this is if foo
> saves the reference to that QString instance.
>
> The only things that setStyleSheet does with the given string is
> assigning its value to another QString and executing a function on it
> (isEmpty()).
>
> > (i'd bet the "" string is catched by kinda "if (sheet.isEmpty())
> return;"
> > statement)
>
> Not quite, if the stylesheet string is empty the original style is
> recreated.
>
> Andreas
>
> --
> Look afar and see the end from the beginning.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>


-- 
 [ signature omitted ] 

Message 7 in thread

you could simply flag it then 

static bool fromSetSheet = false;
MyStyle::polish() {
if (!fromSetSheet) { 
fromSetSheet = true; w->setStyleSheet(""); fromSetSheet = false;
}
}

but maybe there's a more clean solution for your poblem in general...?!

Thomas

Am Montag, 23. Juli 2007 21:54 schrieb Pierre-Étienne Messier:
> Indeed, the problem does not come from setStyleSheet but from the polish()
> function. It seems (from the gdb backtrack) that calling setStyleSheet
> recalls polish, thus creating an infinite loop:
>
> #3391 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950,
> w=0x8b1d530) at styles/qstylesheetstyle.cpp:2180
> #3392 0x00ea6dbc in QWidget::setStyleSheet (this=0x8b1d530,
> styleSheet=@0xac2bec) at kernel/qwidget.cpp:1873
> #3393 0x00abd13e in MyStyle::polish () from
> /home/dev/qt_plugins/styles/libmystyleplugin.so
> #3394 0x010cc9f7 in QStyleSheetStyle::polish (this=0x8b11950, w=0x8b1d530)
> at styles/qstylesheetstyle.cpp:2102
> #3395 0x010c75f0 in updateWidgets (widgets=@0xbf4675e0) at
> styles/qstylesheetstyle.cpp:2041
> #3396 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950,
> w=0x8b1d530) at styles/qstylesheetstyle.cpp:2180
> #3397 0x00ea6dbc in QWidget::setStyleSheet (this=0x8b1d530,
> styleSheet=@0xac2bec) at kernel/qwidget.cpp:1873
> #3398 0x00abd13e in MyStyle::polish () from
> /home/dev/qt_plugins/styles/libmystyleplugin.so
> #3399 0x010cc9f7 in QStyleSheetStyle::polish (this=0x8b11950, w=0x8b1d530)
> at styles/qstylesheetstyle.cpp:2102
> #3400 0x010c75f0 in updateWidgets (widgets=@0xbf467850) at
> styles/qstylesheetstyle.cpp:2041
> #3401 0x010c7a72 in QStyleSheetStyle::repolish (this=0x8b11950,
> w=0x8b1d530) at styles/qstylesheetstyle.cpp:2180
>
> P-E
>
> On 7/23/07, Andreas Pakulat <apaku@xxxxxx> wrote:
> > On 23.07.07 19:24:47, Thomas Lübking wrote:
> > > as qt requires (for whatever reason) a (const) reference  ("&") to
> >
> > a  qstring
> >
> > > as sheet, try
> >
> > Uhm, I think you need to revisit your C++ book, when you do
> >
> > foo("lalala");
> >
> > where foo is declared as
> >
> > void foo(const QString&);
> >
> > then behind your back a QString instance is created (through the const
> > char* constructor) and this QString instance is valid as long as the
> > function executes. The only way to create a segfault with this is if foo
> > saves the reference to that QString instance.
> >
> > The only things that setStyleSheet does with the given string is
> > assigning its value to another QString and executing a function on it
> > (isEmpty()).
> >
> > > (i'd bet the "" string is catched by kinda "if (sheet.isEmpty())
> >
> > return;"
> >
> > > statement)
> >
> > Not quite, if the stylesheet string is empty the original style is
> > recreated.
> >
> > Andreas
> >
> > --
> > Look afar and see the end from the beginning.
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body.
> > List archive and information: http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ] 

Message 8 in thread

yes, know. reason:

"QApplication::QApplication ( int & argc, char ** argv )
Initializes the window system and constructs an application object with argc 
command line arguments in argv.
Warning: The data pointed to by argc and argv must stay valid for the entire 
lifetime of the QApplication object."

(i'd expect this to be mentioned on the qt doc as well, but the the main idea 
could have been to kinda sharing the possibly huge sheet definitions aside 
implicit sharing strings for whatever reason - i'm a bit carefull since i ran 
into the above trap once ;)

Thomas

Am Montag, 23. Juli 2007 21:33 schrieb Andreas Pakulat:
> On 23.07.07 19:24:47, Thomas Lübking wrote:
> > as qt requires (for whatever reason) a (const) reference  ("&") to a 
> > qstring as sheet, try
>
> Uhm, I think you need to revisit your C++ book, when you do
>
> foo("lalala");
>
> where foo is declared as
>
> void foo(const QString&);
>
> then behind your back a QString instance is created (through the const
> char* constructor) and this QString instance is valid as long as the
> function executes. The only way to create a segfault with this is if foo
> saves the reference to that QString instance.
>
> The only things that setStyleSheet does with the given string is
> assigning its value to another QString and executing a function on it
> (isEmpty()).
>
> > (i'd bet the "" string is catched by kinda "if (sheet.isEmpty()) return;"
> > statement)
>
> Not quite, if the stylesheet string is empty the original style is
> recreated.
>
> Andreas

-- 
 [ signature omitted ]