Search code examples
c++qtqt-designerqproperty

Adding a Custom Widget to Qt Q_PROPERTY -- or -- CustomWidgetObjectNames as a property in qt designer custom widget


CustomWidgetObjectNames = design-time objects. enter image description here

Q_PROPERTY(QList<CustomWidgetObjectNames *> points READ getpoints )

QList<CustomWidgetObjectNames *> getpoints() const {
    return sampleobjectlist;
}

private :
QList<sampleobjectlist *>

Solution

  • It feels like a bit home-worky, but you would need something like the code below. I have never tested it myself, and I have not even tried to compile it. However, this should be a good starting point to begin with.

    dynamicpropertyplugin.h

     #ifndef DYNAMICPROPERTYPLUGIN_H
     #define DYNAMICPROPERTYPLUGIN_H
    
     #include <QDesignerCustomWidgetInterface>
    
     class QIcon;
     class QWidget;
    
     class DynamicPropertyPlugin : public QObject, public QDesignerCustomWidgetInterface
     {
         Q_OBJECT
         Q_INTERFACES(QDesignerCustomWidgetInterface)
    
     public:
         DynamicPropertyPlugin(QObject *parent = 0);
    
         QString name() const;
         QString group() const;
         QString toolTip() const;
         QString whatsThis() const;
         QString includeFile() const;
         QIcon icon() const;
         bool isContainer() const;
         QWidget *createWidget(QWidget *parent);
         bool isInitialized() const;
         void initialize(QDesignerFormEditorInterface *formEditor);
         QString domXml() const;
    
     private:
         bool initialized;
     };
    
     #endif
    

    dynamicpropertyextension.h

    class DynamicPropertyExtension : public QObject, public QDesignerDynamicPropertySheetExtension
    {
        Q_OBJECT
        Q_INTERFACES(QDesignerDynamicPropertySheetExtension)
    public:
        DynamicPropertyExtension(QObject *parent = 0);
    
        int addDynamicProperty(const QString & propertyName, const QVariant & value);
        bool canAddDynamicProperty(const QString & propertyName) const;
        bool dynamicPropertiesAllowed() const;
        bool isDynamicProperty(int index) const;
        bool removeDynamicProperty(int index);
    };
    

    dynamicpropertyplugin.cpp

    #include <QtDesigner>
    #include <QtGui>
    #include <QtPlugin>
    
    #include "dynamicproperty.h"
    #include "dynamicpropertyplugin.h"
    #include "dynamicpropertyextension.h"
    
    DynamicPropertyPlugin::DynamicPropertyPlugin(QObject *parent)
        : QObject(parent)
    {
        initialized = false;
    }
    
    QString DynamicPropertyPlugin::name() const
    {
        return "DynamicProperty";
    }
    
    QString DynamicPropertyPlugin::group() const
    {
        return "Display Widgets [Examples]";
    }
    
    QString DynamicPropertyPlugin::toolTip() const
    {
        return "";
    }
    
    QString DynamicPropertyPlugin::whatsThis() const
    {
        return "";
    }
    
    QString DynamicPropertyPlugin::includeFile() const
    {
        return "dynamicproperty.h";
    }
    
    QIcon DynamicPropertyPlugin::icon() const
    {
        return QIcon();
    }
    
    bool DynamicPropertyPlugin::isContainer() const
    {
        return false;
    }
    
    QWidget *DynamicPropertyPlugin::createWidget(QWidget *parent)
    {
        DynamicProperty *dynamicProperty = new DynamicProperty(parent);
        dynamicProperty->setState("-X-XO----");
        return dynamicProperty;
    }
    
    bool DynamicPropertyPlugin::isInitialized() const
    {
        return initialized;
    }
    
    void DynamicPropertyPlugin::initialize(QDesignerFormEditorInterface *formEditor)
    {
        if (initialized)
            return;
    
        QExtensionManager *manager = designerFormEditorInterface->extensionManager();
        Q_ASSERT(manager != 0);
    
        manager->registerExtensions(new DynamicPropertyExtensionFactory(manager),
                                    Q_TYPEID(QDesignerDynamicPropertySheetExtension));
    
        initialized = true;
    }
    
    QString DynamicPropertyPlugin::domXml() const
    {
         return QLatin1String("\
     <ui language=\"c++\">\
         <widget class=\"DynamicProperty\" name=\"dynamicProperty\"/>\
         <customwidgets>\
             <customwidget>\
                 <class>DynamicProperty</class>\
                 <propertyspecifications>\
                 <stringpropertyspecification name=\"state\" notr=\"true\" type=\"singleline\"/>\
                 </propertyspecifications>\
             </customwidget>\
         </customwidgets>\
     </ui>");
     }
    
    
     Q_EXPORT_PLUGIN2(dynamicpropertyextension, DynamicPropertyPlugin
    

    dynamicpropertyextension.h

     #ifndef DYNAMICPROPERTYEXTENSION_H
     #define DYNAMICPROPERTYEXTENSION_H
    
     #include <QDesignerDynamicPropertySheetExtension>
     #include <QExtensionFactory>
    
     class QAction;
     class QExtensionManager;
    
     class DynamicProperty;
    
     class DynamicPropertyExtension : public QObject, public QDesignerDynamicPropertySheetExtension
     {
         Q_OBJECT
         Q_INTERFACES(QDesignerDynamicPropertySheetExtension)
    
     public:
         DynamicPropertyExtension(DynamicPropertyExtenion *tic, QObject *parent);
    
         QAction *preferredEditAction() const;
         QList<QAction *> taskActions() const;
    
     private slots:
         void editState();
    
     private:
         QAction *editStateAction;
         DynamicProperty *dynamicProperty;
     };
    
     class DynamicPropertyExtensionFactory : public QExtensionFactory
     {
         Q_OBJECT
    
     public:
         DynamicPropertyExtensionFactory(QExtensionManager *parent = 0);
    
     protected:
         QObject *createExtension(QObject *object, const QString &iid, QObject *parent) const;
     };
    
     #endif
    

    dynamicpropertyextension.cpp

    #include <QtDesigner>
    #include <QtGui>
    
    #include "dynamicproperty.h"
    #include "dynamicpropertydialog.h"
    #include "dynamicpropertyextension.h"
    
    DynamicPropertyExtension::DynamicPropertyExtension(DynamicProperty *dp, QObject *parent)
        : QObject(parent)
     {
         dynamicProperty = dp;
    
         editStateAction = new QAction(tr("Edit State..."), this);
         connect(editStateAction, SIGNAL(triggered()), this, SLOT(editState()));
     }
    
     void DynamicPropertyExtension::editState()
     {
         DynamicPropertyDialog dialog(ticTacToe);
         dialog.exec();
     }
    
     QAction *DynamicPropertyExtension::preferredEditAction() const
     {
         return editStateAction;
     }
    
     QList<QAction *> DynamicPropertyExtension::taskActions() const
     {
         QList<QAction *> list;
         list.append(editStateAction);
         return list;
     }
    
     DynamicPropertyExtensionFactory::DynamicPropertyExtensionFactory(QExtensionManager *parent)
         : QExtensionFactory(parent)
     {
     }
    
     QObject *DynamicPropertyExtensionFactory::createExtension(QObject *object,
                                                        const QString &iid,
                                                        QObject *parent) const
     {
         if (iid != Q_TYPEID(QDesignerDynamicPropertySheetExtension))
             return 0;
    
         if (DynamicProperty *dp = qobject_cast<DynamicProperty*>(object))
             return new DynamicPropertyExtension(dp, parent);
    
         return 0;
     }
    

    Since it is a designer plugin, you will need to assign the following configuration to qmake in the projet file:

    CONFIG  += designer plugin