template<class T>class Document : public T {....};class mywidget : public Document<QPlainTextEdit> {...};
MainWindow::MainWindow(){ connect(qApp, SIGNAL(focusChanged(QWidget*,QWidget*)) , this, SLOT(focusChanged(QWidget*,QWidget*)));}void checkActionForWidget(QWidget *wdg, QAction *action){ QString signature = QString(action->objectName()).remove("Action"); signature += "()"; QByteArray methodName = QMetaObject::normalizedSignature(signature.toLatin1()); int methodId; if ((methodId = wdg->metaObject()->indexOfMethod(methodName)) != -1) { action->setEnabled(true); } else { action->setEnabled(false); }}void MainWindow::focusChanged(QWidget* old, QWidget* now){ Q_UNUSED(old); if (!now) return ; //QMetaMethod method; checkActionForWidget(now, ui->copyAction); checkActionForWidget(now, ui->cutAction); checkActionForWidget(now, ui->pasteAction);}void MainWindow::actionTriggered(){ QAction *action = qobject_cast<QAction *>(sender()); QWidget *now = qApp->focusWidget(); QMetaMethod method; QString signature = QString(action->objectName()).remove("Action"); signature += "()"; QByteArray methodName = QMetaObject::normalizedSignature(signature.toLatin1()); int methodId; if ((methodId = now->metaObject()->indexOfMethod(methodName)) != -1) { method = now->metaObject()->method(methodId); method.invoke(now); }}