[Qt-interest] Overwriting public slots and qobject_cast
John McClurkin
jwm at nei.nih.gov
Thu Jun 4 18:24:30 CEST 2009
Á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.
>
More information about the Qt-interest-old
mailing list