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

Qt-interest Archive, May 2007
Custom style and colors problem


Message 1 in thread

I have my own style (derived from QPlastiqueStyle) which enforces the 
use of specific colors for all the widgets (a company policy).  Call it 
MyPlastiqueStyle.

There are cases where a specific widget needs to override the default 
colors scheme.  In that case, say for a table widget, I am able to 
change the style to a QPlastiqueStyle instance and set my custom 
colors.  However, I want the scrollbars to be a different set of 
colors.  I am having trouble getting what I want and was wondering if 
any of you could help.

I have a singleton class (MyColors) which holds all the palettes for the 
various colors.  The methods for accessing those palettes are static.  
The style uses these palettes in ::polish.

Here is MyStyle::polish implementation:

    void MyPlastiqueStyle::polish(QWidget *widget)
    {
        // Use the right palette for the right QWidget
        //
        QPlastiqueStyle::polish(widget);
            
        if (widget->inherits("QAbstractButton") ||
            widget->inherits("QComboBox")       ||
            widget->inherits("QAbstractSpinBox"))
        {
            QPalette p = MyColors::selectableButtonPalette();
            widget->setPalette(p);

            if (widget->inherits("QComboBox"))
            {
                QAbstractItemView *v =
                    (qobject_cast<QComboBox *>(widget))->view();

                v->setPalette(p);

                myComboViews.insert(v);

                connect(
                    v,    SIGNAL(destroyed            (QObject *)),
                    this, SLOT  (onComboListDestroyed (QObject *)));
            }
        }
        else if (widget->inherits("QTableView") ||  
            widget->inherits("QTreeView") ||  
            widget->inherits("QListView"))
        {
            if (myComboViews.contains(widget))
                    return;

            widget->setPalette(MyColors::tablePalette());
        }
        else if (widget->inherits("QAbstractItemView"))
        {
            widget->setPalette(MyColors::selectableButtonPalette());
        }
        else if (widget->inherits("QTabBar"))
        {
            widget->setPalette(MyColors::tabPalette());
        }
        else if (widget->inherits("QLineEdit"))
        {
            widget->setPalette(MyColors::editPalette());
        }
        else if (widget->inherits("MyWidgetBase") ||
                 widget->inherits("QDialog"))
        {
            widget->setPalette(MyColors::dialogPalette());
        }
    }


Now here is a table widget implementation that wants to override the 
colors of the cells, but not the scrollbar:

    class MyTableWidget: public QTableWidget
    {
    public:
        MyTableWidget(int rows, int columns, QWidget *parent = NULL)
          :
            QTableWidget(rows, columns, parent)
        {
            MyPlastiqueStyle *curStyle =
                qobject_cast<MyPlastiqueStyle *>(style());
            
            // Replace the style with one that has color
            // enforcement
            //
            if (curStyle)  // We are in the plastique style...
            {
                QPlastiqueStyle *newStyle = new QPlastiqueStyle();
                setStyle(newStyle);
            }
            
            QPalette p = palette();
            p.setColor(
                QPalette::Base,
                Qt::black);
            p.setColor(
                QPalette::Text,
                Qt::yellow);

            setPalette(p);


            // But keep the scrollbars adhering to the color scheme
            //
            if (curStyle)  // We are in the plastique style...
            {
                curStyle = new MyPlastiqueStyle();
                QScrollBar *sb = verticalScrollBar();
                sb->setStyle(curStyle);

                sb = horizontalScrollBar();
                sb->setStyle(curStyle);
            }
        }
    };


Changing the scrollbar's style doesn't seem to work.  Nor does changing 
the palette:
                p = MyColors::dialogPalette();
        p.setColor(QPalette::Base, MyColors::getColor(EditBG));
        p.setColor(QPalette::Text, MyColors::getColor(EditText));

        QScrollBar *sb = verticalScrollBar();
        sb->setPalette(p);

        sb = horizontalScrollBar();
        sb->setPalette(p);

Any ideas what I need to do?

Thanks in advance,
Susan Macchia


---

This communication contains confidential information. If you are not the intended recipient please return this email to the sender and delete it from your records.

Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese an den Absender zurück und löschen Sie die E-mail aus Ihrem System.


Message 2 in thread

It turns out that setting the palette on the scrollbars does work, I was 
missing setting Button and ButtonText in MyColors::dialogPalette().  But 
I still don't understand why setting the style does *not* seem to work. 

So the real question is this:  If I have a composite view, like 
QTableView, which is using MyPlastiqueStyle, and I change it to 
QPlastiqueStyle, why can't I change the style of subwidgets to another 
style?

I realize that I may get comments like: "The style should be consistent, 
..." and in general I absolutely agree.  But I am constrained by the 
type of application I am developing and we are using one style on one 
platform.  So If I am within the derivation of that style, switching 
styles is far easier to maintain than setting individual palettes back 
and forth.

Does anyone know why the style switching on subwidgets in a composite 
view doesn't seem to work?  Or am I simply missing something?

Thanks,
Susan

Susan Macchia wrote:
> I have my own style (derived from QPlastiqueStyle) which enforces the 
> use of specific colors for all the widgets (a company policy).  Call 
> it MyPlastiqueStyle.
>
> There are cases where a specific widget needs to override the default 
> colors scheme.  In that case, say for a table widget, I am able to 
> change the style to a QPlastiqueStyle instance and set my custom 
> colors.  However, I want the scrollbars to be a different set of 
> colors.  I am having trouble getting what I want and was wondering if 
> any of you could help.
>
> I have a singleton class (MyColors) which holds all the palettes for 
> the various colors.  The methods for accessing those palettes are 
> static.  The style uses these palettes in ::polish.
>
> Here is MyStyle::polish implementation:
>
>     void MyPlastiqueStyle::polish(QWidget *widget)
>     {
>         // Use the right palette for the right QWidget
>         //
>         QPlastiqueStyle::polish(widget);
>             
>         if (widget->inherits("QAbstractButton") ||
>             widget->inherits("QComboBox")       ||
>             widget->inherits("QAbstractSpinBox"))
>         {
>             QPalette p = MyColors::selectableButtonPalette();
>             widget->setPalette(p);
>
>             if (widget->inherits("QComboBox"))
>             {
>                 QAbstractItemView *v =
>                     (qobject_cast<QComboBox *>(widget))->view();
>
>                 v->setPalette(p);
>
>                 myComboViews.insert(v);
>
>                 connect(
>                     v,    SIGNAL(destroyed            (QObject *)),
>                     this, SLOT  (onComboListDestroyed (QObject *)));
>             }
>         }
>         else if (widget->inherits("QTableView") ||  
>             widget->inherits("QTreeView") ||  
>             widget->inherits("QListView"))
>         {
>             if (myComboViews.contains(widget))
>                     return;
>
>             widget->setPalette(MyColors::tablePalette());
>         }
>         else if (widget->inherits("QAbstractItemView"))
>         {
>             widget->setPalette(MyColors::selectableButtonPalette());
>         }
>         else if (widget->inherits("QTabBar"))
>         {
>             widget->setPalette(MyColors::tabPalette());
>         }
>         else if (widget->inherits("QLineEdit"))
>         {
>             widget->setPalette(MyColors::editPalette());
>         }
>         else if (widget->inherits("MyWidgetBase") ||
>                  widget->inherits("QDialog"))
>         {
>             widget->setPalette(MyColors::dialogPalette());
>         }
>     }
>
>
> Now here is a table widget implementation that wants to override the 
> colors of the cells, but not the scrollbar:
>
>     class MyTableWidget: public QTableWidget
>     {
>     public:
>         MyTableWidget(int rows, int columns, QWidget *parent = NULL)
>           :
>             QTableWidget(rows, columns, parent)
>         {
>             MyPlastiqueStyle *curStyle =
>                 qobject_cast<MyPlastiqueStyle *>(style());
>             
>             // Replace the style with one that has color
>             // enforcement
>             //
>             if (curStyle)  // We are in the plastique style...
>             {
>                 QPlastiqueStyle *newStyle = new QPlastiqueStyle();
>                 setStyle(newStyle);
>             }
>             
>             QPalette p = palette();
>             p.setColor(
>                 QPalette::Base,
>                 Qt::black);
>             p.setColor(
>                 QPalette::Text,
>                 Qt::yellow);
>
>             setPalette(p);
>
>
>             // But keep the scrollbars adhering to the color scheme
>             //
>             if (curStyle)  // We are in the plastique style...
>             {
>                 curStyle = new MyPlastiqueStyle();
>                 QScrollBar *sb = verticalScrollBar();
>                 sb->setStyle(curStyle);
>
>                 sb = horizontalScrollBar();
>                 sb->setStyle(curStyle);
>             }
>         }
>     };
>
>
> Changing the scrollbar's style doesn't seem to work.  Nor does 
> changing the palette:
>                 p = MyColors::dialogPalette();
>         p.setColor(QPalette::Base, MyColors::getColor(EditBG));
>         p.setColor(QPalette::Text, MyColors::getColor(EditText));
>
>         QScrollBar *sb = verticalScrollBar();
>         sb->setPalette(p);
>
>         sb = horizontalScrollBar();
>         sb->setPalette(p);
>
> Any ideas what I need to do?
>
> Thanks in advance,
> Susan Macchia
>
> ---
> This communication contains confidential information. If you are not 
> the intended recipient please return this email to the sender and 
> delete it from your records.
>
> Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht 
> der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese 
> an den Absender zurück und löschen Sie die E-mail aus Ihrem System.



---

This communication contains confidential information. If you are not the intended recipient please return this email to the sender and delete it from your records.

Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese an den Absender zurück und löschen Sie die E-mail aus Ihrem System.