Search code examples
c++qtc++11qt4qt5

QUiLoader cast from QWidget* to QDialog*, possible?


It seems I'm missing something here. Shouldn't be possible to cast from QWidget to QDialog?

QUiLoader loader;
QFile file("../../plugin/ui/settings.ui");
file.open(QFile::ReadOnly);
m_settingsDialog = qobject_cast<QDialog*>(loader.load(&file));
if(!m_settingsDialog)
    qDebug() << "invalid!";
file.close();

I keep getting it invalid.


Solution

  • Apparently your settings.ui file doesn't describe a QDialog, because the loader doesn't create one (hence the dynamic cast failure).

    If you control the ui file and are the only user, you can change it to define a QDialog.

    If you don't, you can embed the loaded widget in a QDialog:

    m_settingsDialog = new QDialog;
    QWidget settingsWidget = loader.load(&file);
    settingsWidget.setParent(m_settingsDialog);