Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
How i can create .bundle extension file using Qt creator example(Myplug.bundle). I am in a work to create a plugin to already existing application that applications plugins are in bundle format. i found some sample code in Xcode to create bundle .How I can create such a project using QtCreator
Posted

1 solution

XML
Before we can use custom widgets in Qt Designer, we must make Qt Designer aware of them. There are two techniques for doing this: the "promotion" approach and the plugin approach.

Promotion approach
The promotion approach is the quickest and easiest. It consists of choosing a built-in Qt widget that has a similar API to the one we want our custom widget to have and completing a dialog box in Qt Designer with some information about the custom widget. The widget can then be used in forms developed with Qt Designer, although it will be represented by the associated built-in Qt widget while the form is edited or previewed.

Here's how to insert a HexSpinBox widget into a form using this approach:

(1) Create a QSpinBox by dragging it from Qt Designer's widget box onto the form.

(2) Right-click the spin box and choose Promote to Custom Widget from the context menu.

(3) Fill in the dialog that pops up with "HexSpinBox" as the class name and "hexspinbox.h" as the header file.

Thats all! The code generated by uic will include hexspinbox.h instead of <QSpinBox> and instantiate a HexSpinBox. In Qt Designer, the HexSpinBox widget will be represented by a QSpinBox, allowing us to set all the properties of a QSpinBox (for example, the range and the current value).

Plug-in approach
The drawbacks of the promotion approach are that properties that are specific to the custom widget aren't accessible in Qt Designer and that the widget isn't rendered as itself. Both these problems can be solved by using the plugin approach.

The plugin approach requires the creation of a plugin library that Qt Designer can load at run-time and use to create instances of the widget. The real widget is then used by Qt Designer when editing the form and for previewing, and thanks to Qt's meta-object system,Qt Designer can dynamically obtain the list of its properties. To show how this works, we will integrate the IconEditor from the previous section as a plugin.

(1) First, we must subclass QDesignerCustomWidgetInterface and reimplement some virtual functions. 

#include <qdesignercustomwidgetinterface>
class MyPlugin : public QObject,
                         public QDesignerCustomWidgetInterface
{
    Q_OBJECT
    Q_INTERFACES(QDesignerCustomWidgetInterface) // i.e. will be plugin
public:
    MyPlugin(QObject *parent = 0);
    QString name() const;
    QString includeFile() const;
    QString group() const;
    QIcon icon() const;
    QString toolTip() const;
    QString whatsThis() const;
    bool isContainer() const;
    QWidget *createWidget(QWidget *parent);
};

(2) Let's define constructor and function from scope of your class

MyPlugin::MyPlugin(QObject *parent)
    : QObject(parent)
{
}


The constructor is trivial.

QString MyPlugin::name() const
{
    return "TheMyPlugin";
}


The name() function returns the name of the widget provided by the plugin.

QString MyPlugin::includeFile() const
{
    return "myplugin.h";
}


The includeFile() function returns the name of the header file for the specified widget encapsulated by the plugin. The header file is included in the code generated by the uic tool.

QString MyPlugin::group() const
{
    return tr("Image Manipulation Widgets");
}


The group() function returns the name of the widget box group this custom widget should belong to. If the name isn't already in use,Qt Designer will create a new group for the widget.

QIcon MyPlugin::icon() const
{
    return QIcon(":/images/myplugin.png");
}


The icon() function returns the icon to use to represent the custom widget in Qt Designer's widget box. Here, we assume that the MyPlugin has an associated Qt resource file with a suitable entry for the icon editor image.

QString MyPlugin::toolTip() const
{
    return tr("The my new plugin");
}


The toolTip() function returns the tooltip to show when the mouse hovers over the custom widget in Qt Designer's widget box.

QString MyPlugin::whatsThis() const
{
    return tr("This widget is bla-blya-blya");
}


The whatsThis() function returns the "What's This?" text for Qt Designer to display.

bool MyPlugin::isContainer() const
{
    return false;
}

The isContainer() function returns true if the widget can contain other widgets; otherwise, it returns false. For example, QFrame is a widget that can contain other widgets. In general, any Qt widget can contain other widgets, but Qt Designer disallows this when isContainer() returns false.

QWidget *MyPlugin::createWidget(QWidget *parent)
{
    return new MyPlugin(parent);
}


(3)At the end of the source file that implements the plugin class, we must use the Q_EXPORT_PLUGIN2() macro to make the plugin available to Qt Designer. The first argument is the name we want to give the plugin; the second argument is the name of the class that implements it.

Q_EXPORT_PLUGIN2(myplugin, MyPlugin)

(4) The .pro file for building the plugin looks like this:
TEMPLATE        = lib
CONFIG         += designer plugin release
HEADERS         = ...//add path to all headers files
SOURCES         = ...//add path to all cpp files
RESOURCES       = myplugin.qrc // some resource file
DESTDIR         = $(QTDIR)/plugins/designer

(5) Compile/Build it

(6) Copy all following files : *.lib, *.exp, *.dll to ($QTDIR)\plugins\designer 

(7) Add this path to System Variables Path or to program environment  

(8) Run the Qt Designer or only open any ui file in the VCx,in "Widget Box" window, under your section ,see your new plug-in 


P.S. For more information about plugin , please see the documentation of Qt: 
     http://doc.qt.nokia.com/4.7-snapshot/plugins-howto.html 
</qdesignercustomwidgetinterface>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900