PDA

View Full Version : How to override setEnabled(..) method of QWidget ?



anwar.qt
23rd November 2012, 06:02
Hi,

I have classes inherited from QWidget.

class MyWidget : public QWidget
{
----------- Methods -------
public slots :
void setEnabled(bool value);

}


QWidget* myWidget = new MyWidget(...);

myWidget->setEnabled(false);
// this will call to setEnabled(..) of QWidget I want it to callhere MyWidget's setEnabled(...).


Please let me know if I am missing something here.

wysota
23rd November 2012, 07:42
What do you need it for?

Urthas
30th November 2012, 05:41
Please let me know if I am missing something here.

You are missing something here.

...

...

...

but you knew that. :) Here it is: for polymorphism to work, the overridden method has to be virtual (or pure virtual) in the parent class, which is not the case for QWidget::setEnabled(). In addition, you have to call the method using a pointer (or reference, i.e. address-of operator "&"), as you have done. So, you could sub-class QWidget and declare a virtual setEnabled() method, then sub-class that class and implement setEnabled() there. If this is too much bother for a polymorphic setEnabled(), then you'll have to cast myWidget back to a MyWidget * and then call setEnabled(), as follows:


dynamic_cast<MyWidget *>(myWidget)->setEnabled(true);

anda_skoa
30th November 2012, 17:37
When a widget's enabled property is changed, it gets a change event.
What you could do is override QWidget::changeEvent and call your setEnabled whenever the base class' enabled gets changed.

Cheers,
_