Reputation: 107
I know, this is basically the same question, but my problem goes further.
The following tree explains my structure:
QWidget
|
CustomWidget
| |
MyTable MyWidgetAroundIt
I have promoted MyTable
in Qt Designer. So, I can add it to MyWidgetAroundIt
. That worked quite well. The only problem is, CustomWidget
requires it's parent to be a CustomWidget
too, its constructor looks like:
CustomWidget(CustomWidget* parent) : QWidget(parent), _specialValue(parent->getSpecialValue)
This causes compile errors, as the designer generated code tries to initialize MyTable
with a QWidget*
, instead of the CustomWidget*
. What could/should I do to prevent this and/or give the designer a hint about this requirement?
Upvotes: 2
Views: 1461
Reputation: 98425
A widget whose parent can't be a QWidget
is not a widget anymore. Your design breaks the Liskov Substitution Principle and has to be fixed.
You're free to enable special functionality if the widget happens to be of a certain type, but a widget must be usable with any widget for a parent.
Thus:
CustomWidget(QWidget* parent = nullptr) :
QWidget(parent)
{
auto customParent = qobject_cast<CustomWidget*>(parent);
if (customParent)
_specialValue = customParent->specialValue();
}
or:
class CustomWidget : public QWidget {
Q_OBJECT
CustomWidget *_customParent = qobject_cast<CustomWidget*>(parent());
SpecialType _specialValue = _customParent ? _customParent->specialValue() : SpecialType();
SpecialType specialValue() const { return _specialValue; }
public:
CustomWidget(QWidget * parent = nullptr) : QWidget(parent) {}
};
Upvotes: 3