PDA

View Full Version : Some problems about using custom widget in designer!



furskytl
18th September 2011, 15:11
I want to add my customed widget to designer,but it doesn't work!
It said that in the function QWidget * NeonLightPlugin::createWidget(QWidget * parent) cannot convert 'NeonLightPlugin*' to 'Widget*'.
And it also said error: expected constructor, destructor, or type conversion before '(' token in the last line Q_EXPORT_PLUGIN2(NeonLight, NeonLightPlugin)!!!
What is wrong with it???
Thanks very much!

The plugin files are here:
neonlightplugin.h

#ifndef NEONLIGHTPLUGIN_H
#define NEONLIGHTPLUGIN_H
#include <QObject>
#include <QIcon>
#include <QWidget>
#include <QString>
#include <QDesignerCustomWidgetInterface>

class NeonLightPlugin:public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
NeonLightPlugin(QObject * parent = 0);

bool isContainer() const;
QIcon icon() const;
QString group() const;
QString includeFile() const;
QString name() const;
QString toolTip() const;
QString whatsThis() const;
QWidget *createWidget(QWidget *parent);
};

#endif // NEONLIGHTPLUGIN_H

And the neonlightplugin.cpp


#include <QWidget>
#include "neonlightplugin.h"

NeonLightPlugin::NeonLightPlugin(QObject *parent)
{
}

QString NeonLightPlugin::name() const
{
return "NeonLight";
}

QString NeonLightPlugin::includeFile() const
{
return "neonlight.h";
}

QString NeonLightPlugin::group() const
{
return tr("Special Effect");
}

QIcon NeonLightPlugin::icon() const
{
return QIcon(":/file/resources/light.png");
}

QString NeonLightPlugin::toolTip() const
{
return tr("A neon light widget for special effect");

}

QString NeonLightPlugin::whatsThis()const
{
return tr("This widget is presented by Tan Le");
}

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

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

Q_EXPORT_PLUGIN2(NeonLight, NeonLightPlugin)

norobro
18th September 2011, 22:50
For the first error see comment:

QWidget * NeonLightPlugin::createWidget(QWidget * parent)
{
return new NeonLightPlugin(parent); // you should return your custom widget here
}

furskytl
19th September 2011, 16:21
Yes,thanks!But how about the second one?

norobro
19th September 2011, 21:05
Maybe a missing include? Try:
#include <QtPlugin>

wysota
19th September 2011, 21:11
First argument of Q_EXPORT_PLUGIN2 should be the name of the target library you are building. The macro itself needs to be available by including <QtPlugin>.

furskytl
20th September 2011, 15:23
Yes,now I can build the project succefully!But I still can't see my customed widget in the designer! I run mingw32-make install ,and find that two files are created in the release folder :neonlightplugin.dll and neonlightplugin.a . What should I do to see my widget in the designer?And another question is that whether should I create the plugin in a new project and then use it in another project or both create and use the custom widget in one project?I can't find the details in the book! I have been trapped by this problem for serveral days.Thanks!!!

wysota
20th September 2011, 15:28
Do you have Designer built with MinGW?

furskytl
20th September 2011, 15:42
Do you have Designer built with MinGW?
I don't know! I use the Qt SDk 4.7.0 downloaded from the website.

Added after 5 minutes:


Do you have Designer built with MinGW?
I just now build the example customwidgetplugin with qt.But I can't find it in the designer,either.Am I wrong?How to use the example?I think the example must be correct,so I must do something wrong creating and using the customed widget in the designer.Help me!

wysota
20th September 2011, 16:09
I don't know! I use the Qt SDk 4.7.0 downloaded from the website.
As far as I know it is built with MSVC. You should have a Designer built against MinGW in the Qt's bin subdirectory, It should see your plugin, provided it is copied into the proper place.

furskytl
20th September 2011, 16:41
Yes,thank you very much!!! I go to the qt\bin directory,and run the designer there,then I can see my customed widget!!!I just open the designer item in the Qt Creator before,so I can't see the customed widget!Only now am I aware of the difference between the designer in the Creator and the Qt Designer!!! Thanks very much!!!