CustomWidgetObjectNames =设计时对象。
Q_PROPERTY(QList<CustomWidgetObjectNames *> points READ getpoints )
QList<CustomWidgetObjectNames *> getpoints() const {
return sampleobjectlist;
}
private :
QList<sampleobjectlist *>
答案 0 :(得分:4)
感觉有点家庭式,但你需要类似下面的代码。我自己从未测试过,我甚至都没有尝试过编译它。但是,这应该是一个很好的起点。
#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
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);
};
#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
#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
#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