Добавление пользовательского виджета в Qt Q_PROPERTY — или — CustomWidgetObjectNames как свойство в пользовательском виджете дизайнера qt

CustomWidgetObjectNames = объекты времени разработки.
введите описание изображения здесь

Q_PROPERTY(QList<CustomWidgetObjectNames *> points READ getpoints )

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

private :
QList<sampleobjectlist *>

1

Решение

Похоже, немного по-домашнему, но вам нужно что-то вроде кода ниже. Я никогда не проверял это сам и даже не пытался скомпилировать. Тем не менее, это должно быть хорошей отправной точкой для начала.

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;
}

Поскольку это плагин дизайнера, вам нужно назначить следующую конфигурацию для qmake в файле projet:

CONFIG  += designer plugin
4

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]