I have derived a custom widget my_widget from QWidget. There is a factory function in my MainWindow that creates those widgets, and their destroy-signal is connected to a slot in the MainWindow to do some cleanup.
Problem: qobject_cast returns zero when I try to cast the QOjbect* the slot received to my_widget*.
If I try the cast directly in the factory function, that works.
If I use plain-cast in the slot, it works too (the pointer is the same as returned by tthe factory function).
If I try to qobject_cast to a QWidget*, that works too.
my_widget
* create_my_widget
(QWidget* parent
= nullptr
) {
auto w = new my_widget(parent);
auto sss = qobject_cast<my_widget*>(ppp); //works!
connect(w, &my_widget::destroyed, this, &MainWindow::remove_mywidget_widget);
return w;
}
my_widget* create_my_widget(QWidget* parent = nullptr)
{
auto w = new my_widget(parent);
QObject* ppp = (QObject*)w;
auto sss = qobject_cast<my_widget*>(ppp); //works!
connect(w, &my_widget::destroyed, this, &MainWindow::remove_mywidget_widget);
return w;
}
To copy to clipboard, switch view to plain text mode
slot:
void MainWindow
::remove_mywidget_widget(QObject* removed
) {
auto mywid = qobject_cast<my_widget*>(removed); //Fails!
auto mywid2 = my_widget::staticMetaObject.cast(removed); //Fails!
auto mywid3 = qobject_cast<QWidget*>(removed); //Works!
auto mywid4 = (my_widget*)removed; //Works!
...
}
void MainWindow::remove_mywidget_widget(QObject* removed)
{
auto mywid = qobject_cast<my_widget*>(removed); //Fails!
auto mywid2 = my_widget::staticMetaObject.cast(removed); //Fails!
auto mywid3 = qobject_cast<QWidget*>(removed); //Works!
auto mywid4 = (my_widget*)removed; //Works!
...
}
To copy to clipboard, switch view to plain text mode
Where am I going wrong?
Bookmarks