On Wednesday 14 May 2003 11:10, Filippos Papadopoulos wrote:
I want to override only one method of a widget in Qt library. To do this i create my own class, which inherits that widget, and i declare just that one method in my new class. The problem is that the compiler (g++) complains about the constructors. Do i need also to override ALL the constructors of that widget? (there are many of them). If so, isn't there another way of implementing this? I mean just to override the method that i am interested in and not any other thing in that widget class.
You have to specify constructors for your classes derived of QWidget and
related. Make sure you call the parent class constructor first. Otherwise
most of the Qt internal stuff would not work.
Most Qt classes intentionally don't provide a default constructor (one that
does not receive any argument) because of this.
You don't need all the variety of constructors that Qt classes provide, but at
least one - preferably one that at least accepts a "parent" argument so the
Qt widget hierarchy logic remains intact. As an added benefit, Qt will take
care of cleaning up widgets that have a parent when you delete the parent (or
the grandparent etc.).
Example:
class MyWidget: public QPushButton
{
Q_OBJECT
MyWidget( QWidget * parent );
virtual ~MyWidget();
void myVeryCoolMethodThatDoesFancyThings();
};
Providing a virtual destructor is also a very good idea for classes that
contain dynamically allocated memory (which all QWidgets do) - see
http://cpptips.hyperformix.com/cpptips/why_virt_dtor2
CU
--
Stefan Hundhammer