0

In order to react to an event in the Qt framework I have reimplemented a Qt class (QLabel), i.e. its default constructor with a single argument. Since that gave an error, I also reimplemented the constructor with an additional argument. Now it works.

Class:

class myLabel : public QLabel
{
    Q_OBJECT
public:
    explicit myLabel(QWidget *parent = 0);
    explicit myLabel(QString x, QWidget *parent = 0);
    void mousePressEvent( QMouseEvent* e );
signals:
    void clicked();
};

Constructors:

myLabel::myLabel(QWidget *parent) : QLabel(parent)
{
}
myLabel::myLabel(QString x, QWidget *parent) : QLabel(parent)
{
    setText(x);
}

My (beginner) question is: why isn't the respective constructor of the base class run, as with all other methods [of the base class (e.g. I can use setText() without reimplementing it)]? And the extreme: why do I need to reimplement a constructor at all, although it does not contain any code?

Edit: Before posting, I was not sure what to search for, but this question has essentially been answered in https://stackoverflow.com/a/347362/1619432

Edit: As bartimar pointed out, the constructor is not empty in that it calls the base class' constructor. This can apparently be done with passing the respective parameters like so:

myLabel::myLabel( const QString& x, QWidget* parent, Qt::WindowFlags f ) : 
    QLabel( x, parent, f ) {}
Community
  • 1
  • 1
handle
  • 5,859
  • 3
  • 54
  • 82
  • What do you mean by "as with all other methods"? Also, you *can* inherit constructors, but you need to say that explicitly (like `using QLabel::QLabel;`). – Kerrek SB Jun 03 '13 at 12:34
  • 1
    It DOES contain code... ": QLabel(parent)" means you call QLabel constructor at the start of your constructor, so in fact it does contain code and its not empty. When there isn't this constructor and your constructor is empty, no objects will be created and you have no QLabel to edit. – bartimar Jun 03 '13 at 12:47

0 Answers0