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?
Asked
Active
Viewed 213 times
1

Ralph Tandetzky
- 22,780
- 11
- 73
- 120
-
3It has a default value now in [Qt 5.7](https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qaction.h?h=5.7#n93). However, it did not have a default value in [Qt 5.6](https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qaction.h?h=5.6.2#n87). – Mike Oct 11 '16 at 08:43
-
@Mike Good catch. I'm working with Qt 5.5 and did not know of the change. This explains it. Thanks. – Ralph Tandetzky Oct 11 '16 at 09:34
-
I'm just guessing here, but there could be a reason it was done like this originally. `QWidget::addAction(QAction*)` does not take ownership of the `QAction` object, so maybe this was a way to prevent memory leaks. – thuga Oct 11 '16 at 10:01
1 Answers
3
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.

Kuba hasn't forgotten Monica
- 95,931
- 16
- 151
- 313

Hayt
- 5,210
- 30
- 37