Search code examples
c++qtconstructorqaction

Why does QAction not have a default constructor?


Most classes in Qt have a default constructor. Many classes take a QObject* or QWidget* as constructor argument with default value nullptr. The class QAction also has such a constructor taking a QObject* as argument, but there is no default value. Therefore, QAction is not default constructable. Why is that? Does a QAction really need to have a parent? Or is there anything else different in QAction that explains this inconsistency?


Solution

  • Just a guess here but I would say there is no reason behind it.

    In Qt 4.8 the signature is (as you mentioned) without default value for parent.

    QAction(QObject * parent)
    

    see http://doc.qt.io/qt-4.8/qaction.html

    This changes in Qt 5.7 though

    QAction(QObject *parent = nullptr)
    

    see http://doc.qt.io/qt-5/qaction.html

    So I assume it was an accidental inconsistency which finally got fixed with Qt 5.7.