如何将自己的选项页面添加到QtCreator

时间:2013-09-05 09:47:39

标签: qt qt-creator

我想在QtCreator中添加自定义选项页面。项目向导可以轻松创建一个通用的QtCreator插件。但是,如何从那里开始并添加新功能和新小部件?如果完全记录的话,它只是很薄。关于如何编写我发现的QtCreator插件的唯一教程是来自VCreate Logic的旧pdf文件。

http://www.vcreatelogic.com/downloads/files/Writing-Qt-Creator-Plugins.pdf

然而,这描述了早期QtCreator版本(早期版本1)的插件开发。大多数示例不再使用当前的QtCreator进行编译。

1 个答案:

答案 0 :(得分:7)

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");

} 
} 

结果:自定义选项页面在其自己的类别中有自己的图标:

enter image description here

相关问题