最后,我们再追溯一下前面提到的void ModeManagerPrivate::appendMode(IMode *mode)是哪里调用的?
我们挨个函数挨个函数的向源头追溯,得知:
void ModeManagerPrivate::extensionsInitializedHelper() { for (IMode *mode : m_modes) appendMode(mode); ModeManager::extensionsInitialized() void ModeManager::extensionsInitialized() { d->extensionsInitializedHelper(); } MainWindow::extensionsInitialized() void MainWindow::extensionsInitialized() { ModeManager::extensionsInitialized(); void CorePlugin::extensionsInitialized() void CorePlugin::extensionsInitialized() { m_mainWindow->extensionsInitialized();
最后的void CorePlugin::extensionsInitialized(),在
class EXTENSIONSYSTEM_EXPORT IPlugin类的定义是纯虚函数,需要用户在外部自己实现。
virtual void extensionsInitialized() = 0;
以上就是Qt Creator插件的实现原理。
三、早期的Qt Creator版本在插件接口定义上有不同之处,比如Qt Creator 3.2源码ModeManager有addWidget函数。
class CORE_EXPORT ModeManager : public QObject { static void addWidget(QWidget *widget);
通过addWidget函数可以实现如下效果:
以下源码从开源项目摘录 https://github.com/lexxmark/QtCreator-JsExtensions-Plugin
/jep_plugins/JepUtils.js /jep_plugins/ModePanelButtons/ModePanelButtons.jep.js function createActionToolButton(actionName) { // look for an action var cmd = actionManager.command(actionName); if (cmd === null) { jepAPI.debug("Cannot find such action: " + actionName); return null; } // get action var action = cmd.action(); // create button var bttn = jepAPI.createQObject("QToolButton", null); // tune button bttn.toolButtonStyle = 2; bttn.autoRaise = true; bttn.styleSheet = "QToolButton {color: black; }"; // disable button's minimum width bttn.sizePolicy = jepAPI.sizePolicy(13, 0, 1); // assign action bttn.setDefaultAction(action); return bttn; } // add action buttons to mode panel settings.actions.map(function (actionName) { var bttn = createActionToolButton(actionName); if (bttn) modeManager.addWidget(bttn); });
总之,Qt Creator 4.8.2版本没有ModeManager::addWidget函数,想添加自定义按钮只能通过ModeManager::addAction实现。