QtCreator 极大地帮助创建 QtCreator 插件。它有自己的 QtCreator 插件项目类型。
要创建一个新的选项页面,需要启动一个 QtCreator 插件项目。在此示例中,名称为“myoptionspage”。QtCreator 然后创建一个工作插件存根,它不是一个选项页面,而是一个如何在 QtCreator 菜单中添加新菜单条目的示例。很好,但没有问。要创建新的选项页面myoptionspage::initialize
,必须更改方法:
bool myoptionspage::initialize(const QStringList &arguments,
QString *errorString)
{
Q_UNUSED(arguments)
Q_UNUSED(errorString)
addAutoReleasedObject(new MyMoptionsPageWidget);
return true;
}
MyMoptionsPageWidget 将是实际的选项页面。这是 MyMoptionsPageWidget.h 文件:
#include <coreplugin/dialogs/ioptionspage.h>
class MyMoptionsPageWidget
: public Core::IOptionsPage
{
Q_OBJECT
public:
explicit MyMoptionsPageWidget(QObject *parent = 0);
private:
QWidget *createPage(QWidget *parent);
void apply(void);
void finish();
};
重要的部分是#include <coreplugin/dialogs/ioptionspage.h>
继承
public Core::IOptionsPage
。
在 MyMoptionsPageWidget .cpp 文件中:
using namespace myoptionspage;
MyMoptionsPageWidget::MyMoptionsPageWidget(QObject *parent)
: Core::IOptionsPage(parent)
{
setId(Core::Id("MyOptionsPageID"));
setDisplayName(tr("My Plugin"));
// Create a new category for the options page. Here we create a totally
// new category. In that case we also provide an icon. If we choose in
// 'setCategory' an already existing category, the options page is added
// the chosen category and an additional tab. No icon is set in this case.
setCategory(Constants::MYOPTIONSPAGE_CATEGORY);
setDisplayCategory(QLatin1String(
Constants::MYOPTIONSPAGE_CATEGORY_TR_CATEGORY));
setCategoryIcon(
QLatin1String(Constants::MYOPTIONSPAGE_CATEGORY_CATEGORY_ICON));
}
// Demoform is an arbitrary QWidget. For this example I hacked one
// together with the designer.
QWidget *MyMoptionsPageWidget::createPage(QWidget *parent){
return new Demoform;
}
void MyOptionsPage::apply(){
// Implement the apply botton functionality
}
void MyOptionsPage::finish(){
// Implement the ok button functionality
}
文件 myoptionspageconstants.h 由 QtCreator 自动创建。
namespace myoptionspage {
namespace Constants {
const char MYOPTIONSPAGE_CATEGORY[] = "H.My Plugin";
const char MYOPTIONSPAGE_CATEGORY_CATEGORY_ICON[] = ":resources/Icon.png";
const char MYOPTIONSPAGE_CATEGORY_TR_CATEGORY[] =
QT_TRANSLATE_NOOP("My Plugin", "My Plugin");
}
}
结果:自定义选项页面在其自己的类别中具有自己的图标: