class Application : public QApplication{ Q_OBJECTpublic: explicit Application(int & argc, char ** argv); static bool tryQuit();protected: bool event(QEvent *e);};bool Application::tryQuit(){ QList<QWidget *> widgets = qApp->allWidgets(); foreach (QWidget *widget, widgets) { QDialog *dialog = qobject_cast<QDialog*>(widget); if (dialog) { if (dialog->windowModality() == Qt::WindowModal && dialog->isVisible()) { dialog->raise(); dialog->activateWindow(); dialog->focusWidget()->setFocus(); qApp->beep(); return false; } } } return true;}bool Application::event(QEvent *e){ if (e->type() == QEvent::Close) { if (tryQuit()) e->accept(); else e->ignore(); return true; } return QApplication::event(e);}
QObject parent;QObject* child = new QWidget;child->setParent(&parent); // вызывается QObject::setParent() и ошибка будет только в рантайме при помощи QObject::isWidgetType
if (object->isWidgetType()) { QWidget *w = static_cast<QWidget*>(w); ...}
QWidget *w = qobject_cast<QWidget*>(object);if (w) { ...}
bool Application::event(QEvent *e){ if (e->type() == QEvent::Close) { if (...) e->accept(); else e->ignore(); return true; } return QApplication::event(e);}