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

Qt-interest Archive, June 2007
QPalette + NColorGroups question


Message 1 in thread

Hi all,

I have come across the following QT code ...

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QPalette palSelected;
QPalette palNormal;
...

class KTPushButton : public QPushButton
{
public:
   KTPushButton(QWidget * parent)
     : QPushButton(parent), isSelected(false), isDirty(false)
   {
      updatePalette();
   }

   void updatePalette()
   {
      palNormal = ((QWidget *)parent())->palette();
      palSelected = palNormal;
      for(int cg = (int) QPalette::Disabled; cg < (int)
QPalette::NColorGroups; cg++)
     .....
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I can't get my head around ....

palSelected = (QWidget *)parent())->palette();

I just don't see how the above works (though it does :))  Not sure if
its my misunderstanding of C++ or QT. I would have thought it should
be more like ... (or am I missing the point ?)

palSelected = parent->palette();

Also ...

What does QPalette::NColorGroups return ... the number of color groups
availiable ? If so

      for(int cg = (int) QPalette::Disabled; cg < (int)
QPalette::NColorGroups; cg++)

counts from (int) QPalette::Disabled - that will be a 1 to number of
color groups ?


Cheers, still learning :)

Dave








-- 
 [ signature omitted ] 

Message 2 in thread

On 17.06.07 14:57:49, dave selby wrote:
> QPalette palNormal;
> ...
> 
> class KTPushButton : public QPushButton
> {
> public:
>   KTPushButton(QWidget * parent)
>     : QPushButton(parent), isSelected(false), isDirty(false)
>   {
>      updatePalette();
>   }
> 
>   void updatePalette()
>   {
>      palNormal = ((QWidget *)parent())->palette();
>      palSelected = palNormal;
>      for(int cg = (int) QPalette::Disabled; cg < (int)
> QPalette::NColorGroups; cg++)
>     .....
> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> 
> I can't get my head around ....
> 
> palSelected = (QWidget *)parent())->palette();
> 
> I just don't see how the above works (though it does :))  Not sure if
> its my misunderstanding of C++ or QT. I would have thought it should
> be more like ... (or am I missing the point ?)
> 
> palSelected = parent->palette();

See the API docs, parent() returns a QObject, so you need a cast to
QWidget. Although the above c-cast is bad, its better to use static_cast
or dynamic_cast.

> What does QPalette::NColorGroups return ... the number of color groups
> availiable ? If so
> 
>      for(int cg = (int) QPalette::Disabled; cg < (int)
> QPalette::NColorGroups; cg++)
> 
> counts from (int) QPalette::Disabled - that will be a 1 to number of
> color groups ?

Right.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

On 17/06/07, Andreas Pakulat <apaku@xxxxxx> wrote:
> On 17.06.07 14:57:49, dave selby wrote:
> > QPalette palNormal;
> > ...
> >
> > class KTPushButton : public QPushButton
> > {
> > public:
> >   KTPushButton(QWidget * parent)
> >     : QPushButton(parent), isSelected(false), isDirty(false)
> >   {
> >      updatePalette();
> >   }
> >
> >   void updatePalette()
> >   {
> >      palNormal = ((QWidget *)parent())->palette();
> >      palSelected = palNormal;
> >      for(int cg = (int) QPalette::Disabled; cg < (int)
> > QPalette::NColorGroups; cg++)
> >     .....
> > ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> >
> > I can't get my head around ....
> >
> > palSelected = (QWidget *)parent())->palette();
> >
> > I just don't see how the above works (though it does :))  Not sure if
> > its my misunderstanding of C++ or QT. I would have thought it should
> > be more like ... (or am I missing the point ?)
> >
> > palSelected = parent->palette();
>
> See the API docs, parent() returns a QObject, so you need a cast to
> QWidget. Although the above c-cast is bad, its better to use static_cast
> or dynamic_cast.
>

Ahhh ... penny drops :) thank you :)

> > What does QPalette::NColorGroups return ... the number of color groups
> > availiable ? If so
> >
> >      for(int cg = (int) QPalette::Disabled; cg < (int)
> > QPalette::NColorGroups; cg++)
> >
> > counts from (int) QPalette::Disabled - that will be a 1 to number of
> > color groups ?
>
> Right.

:)

>
> Andreas
>

And everything gets a bit clearer.

Cheers

Dave



> --
> You need more time; and you probably always will.
>
> --
> 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 4 in thread

> > > What does QPalette::NColorGroups return ... the number of color
groups
> > > availiable ? If so
> > >
> > >      for(int cg = (int) QPalette::Disabled; cg < (int)
> > > QPalette::NColorGroups; cg++)
> > >
> > > counts from (int) QPalette::Disabled - that will be a 1 to number
of
> > > color groups ?
> >
> > Right.
> 
> :)
> 
> >
> > Andreas
> >
> 
One thing of note... QPalette::NColorGroups does not "return" the number
of color groups... it IS the number of color groups... There is no
function stack for the call QPalette::NColorGroups

Scott

--
 [ signature omitted ] 

Message 5 in thread

On 6/17/07, Andreas Pakulat <apaku@xxxxxx> wrote:
>
> On 17.06.07 14:57:49, dave selby wrote:
> > QPalette palNormal;
> > ...
> >
> > class KTPushButton : public QPushButton
> > {
> > public:
> >   KTPushButton(QWidget * parent)
> >     : QPushButton(parent), isSelected(false), isDirty(false)
> >   {
> >      updatePalette();
> >   }
> >
> >   void updatePalette()
> >   {
> >      palNormal = ((QWidget *)parent())->palette();
> >      palSelected = palNormal;
> >      for(int cg = (int) QPalette::Disabled; cg < (int)
> > QPalette::NColorGroups; cg++)
> >     .....
> >
> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> >
> > I can't get my head around ....
> >
> > palSelected = (QWidget *)parent())->palette();
> >
> > I just don't see how the above works (though it does :))  Not sure if
> > its my misunderstanding of C++ or QT. I would have thought it should
> > be more like ... (or am I missing the point ?)
> >
> > palSelected = parent->palette();
>
> See the API docs, parent() returns a QObject, so you need a cast to
> QWidget. Although the above c-cast is bad, its better to use static_cast
> or dynamic_cast.


Even better would be to use QWidget::parentWidget(), which works like
QObject::parent() but returns a QWidget*.

Tom