[Qt-interest] Overwriting public slots and qobject_cast

Álvaro Morais alvarommorais at gmail.com
Fri Jun 5 19:26:26 CEST 2009


A Quinta 04 Junho 2009 17:24:30 John McClurkin você escreveu:
> Álvaro Morais wrote:
> > Hi.
> >
> > I want to make a custom progressBar that changes color with
> > percentage. For example 25% will give a highlight red, 50% Yellow,
> > etc...
> >
> > It will also automatically give the password entropy of a QString, so
> > the setValue SLOT will become private, since there is no need to
> > change it from outside.
>
> According to the docs even if a slot is private, another object can
> still connect a signal to it and it will be called when the signal is is
> emitted, so I see little point to trying to make the slot private.
>
> > I'm using the promote to custom widget on the QtDesigner.
> >
> > My question is: What is the best way of overwriting the public slots?
> >
> > is it safe to do this:
> >
> > class PasswordQuality : public QProgressBar
> > {
> > ...
> > private slots:
> >     virtual void setValue ( int value ) {
> > 	qobject_cast<QProgressBar*>(this)->setValue(value);
> >
> > 	... //code to change the palette.
> > 	}
> > };
>
> You don't keed to use qobject_cast here because the this pointer is a
> pointer to a QProgressBar type object. Qobject_cast is used when you are
> not sure when of the type of a pointer to type QWidget. In this
> situation, qobject_cast will return a NULL if the pointer is to an
> incompatible type, for example if you are expecting a pointer to a
> QMainWindow but the pointer received is to a QDialog.
>
> > Am I using qobjet_cast the right way?
> >
> > Thanks in advance.
>
> You are subclassing QProgressBar to add functionality. I would suggest
> an alternative suggestion. Create a new slot, say setEntropy(int), and
> connect that to the signal that normally would be connected to a
> QProgreasBar setValue slot. Then, in setEntropy do the color change and
> call QProgressBar setValue.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

The objective of sub-classing in this example is for the learning experience 
of QT since I'm relatively new to it. I will probably later change for your 
suggestion. As I said in the previous post I had bad assumptions based on an 
execution error. I figured this out.

Thanks for your reply and clarification. Your explanation about qobject_cast 
was particularly useful.

Álvaro Morais

-- 
Neither Emacs or Vi are my primary editors...
..and I'm not ashamed!!!

alvarommorais at gmail.com - Lisbon - Portugal




More information about the Qt-interest-old mailing list