1

CustomWidgetObjectNames = 设计时对象。 在此处输入图像描述

Q_PROPERTY(QList<CustomWidgetObjectNames *> points READ getpoints )

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

private :
QList<sampleobjectlist *>
4

1 回答 1

4

感觉有点像家庭作业,但你需要类似下面的代码。我自己从来没有测试过它,我什至没有尝试编译它。但是,这应该是一个很好的起点。

动态属性插件.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

动态属性扩展.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);
};

动态属性插件.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

动态属性扩展.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

动态属性扩展.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;
 }

由于它是一个设计器插件,您需要在 projet 文件中为 qmake 分配以下配置:

CONFIG  += designer plugin
于 2013-12-30T09:41:39.247 回答