PDA

View Full Version : Insert custom widget in the qt designer list box



eg3gg
5th September 2010, 17:18
I created a custom plugin subclassing QWidget, then I created a plugin subclassing QDesignerCustomWidgetInterface and using this same header as an interface

The plugin works correctly in both static and dynamic configurations if manually loaded by code, but I can't figure out how to show it into the Qt Designer's list box.

I tried creating two classes like this example
http://doc.trolltech.com/4.5/designer-taskmenuextension.html
but I failed, I tried to include a .pri file into the project but I failed too. I put the dll/lib files of my plugin into the C:\Qt\2010.04\qt\plugins\designer (I am under WinXp) but the plugin won't show up in the list box of the Qt Designer.

Any suggestions? Tell me if a class' code is needed to paste, I can't figure out where's the problem

SixDegrees
5th September 2010, 17:22
I'm not a Windows programmer, but if you can run designer from a console/terminal, it spews out all sorts of useful information on startup about various problems it encounters. Normally, it will provide you with clues about why your plugin is failing to load. If it doesn't, it means it isn't find it to begin with, but that's probably not the problem.

Perhaps there's a designer startup log under Windows?

wysota
5th September 2010, 17:25
Please don't confuse "class" with "plugin". You should create a plugin containing an implementation of the QDesignerCustomWidgetInterface class (and appropriate plugin export macro as said in the docs). The class should return instances of a custom widget class. The plugin should be deployed in $QTDIR/plugins/designer directory.

eg3gg
5th September 2010, 19:28
I missed the EXPORT macro. I just have to include it into the plugin declaration class right?

Not into the base QWidget class, at least documentation seems telling this

Regarding the plugin debugging i found this:

set QT-DEBUG-PLUGINS=1 in your shell.

But in Win32 I really don't know how to log this or where to insert it... Perhaps using qmake manually by console?

eg3gg
5th September 2010, 20:09
Update: added the export macro like following

...
#include <QtDesigner/QDesignerExportWidget>

class QDESIGNER_WIDGET_EXPORT IconEditor : public QWidget
{
...

If I compile only the custom widget project (not the plugin) I have a lot of errors:

undefined reference to _imp__xxxx
undefined reference to _imp__xxxx
undefined reference to _imp__xxxx
undefined reference to _imp__xxxx
undefined reference to _imp__xxxx
collect2: ld returned 1 exit status

But if I compile the plugin project it succeeds. So I think I did it right.

After compiling I find the iconeditorplugin.dll and libiconeditorplugin.a in the $QTDIR/plugins/designer (C:\Qt\2010.04\qt\plugins\designer to be precise). As documentation states.

Then I open the Qt Creator and create a generic dialog GUI and I still can't find my plugin (it has everything I think, including icons and descriptions) in the list box. To clear every doubt about where I want to show my plugin, I am including this screenshot:

- Screenshot - (http://img839.imageshack.us/img839/574/ssssssv.jpg)

Regarding the QT-DEBUG-PLUGINS=1, I set the environment variable but where to see a debug log?? I can't find any errors anywhere.

I'm a newbie to Qt (you probably would have got it yet) but I'm reading through all the documentation and still can't figure out how to solve

wysota
5th September 2010, 20:31
You need Q_EXPORT_PLUGIN macro to actually export the plugin. Do you have it?

eg3gg
6th September 2010, 07:41
Yes, I'm pasting the content of the plugin files

iconeditorplugin.cpp

#include <QtPlugin>

#include "../iconeditor/iconeditor.h"
#include "iconeditorplugin.h"

IconEditorPlugin::IconEditorPlugin(QObject *parent)
: QObject(parent)
{
}

QString IconEditorPlugin::name() const
{
return "IconEditor";
}

QString IconEditorPlugin::includeFile() const
{
return "iconeditor.h";
}

QString IconEditorPlugin::group() const
{
return tr("Image Manipulation Widgets");
}

QIcon IconEditorPlugin::icon() const
{
return QIcon(":/images/iconeditor.png");
}

QString IconEditorPlugin::toolTip() const
{
return tr("An icon editor widget");
}

QString IconEditorPlugin::whatsThis() const
{
return tr("This widget is presented in Chapter 5 of <i>C++ GUI "
"Programming with Qt 4</i> as an example of a custom Qt "
"widget.");
}

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

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

Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin)

iconeditorplugin.h

#ifndef ICONEDITORPLUGIN_H
#define ICONEDITORPLUGIN_H

#include <QtDesigner/QDesignerCustomWidgetInterface>

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

public:
IconEditorPlugin(QObject *parent = 0);

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



};

#endif

iconeditor.h

#ifndef ICONEDITOR_H
#define ICONEDITOR_H

#include <QColor>
#include <QImage>
#include <QWidget>
#include <QtDesigner/QDesignerExportWidget>

//QDESIGNER_WIDGET_EXPORT
class QDESIGNER_WIDGET_EXPORT IconEditor : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor penColor READ penColor WRITE setPenColor)
Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage)
Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor DESIGNABLE true)

public:
IconEditor(QWidget *parent = 0);

void setPenColor(const QColor &newColor);
QColor penColor() const { return curColor; }
void setZoomFactor(int newZoom);
int zoomFactor() const { return zoom; }
void setIconImage(const QImage &newImage);
QImage iconImage() const { return image; }
QSize sizeHint() const;

protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);

private:
void setImagePixel(const QPoint &pos, bool opaque);
QRect pixelRect(int i, int j) const;

QColor curColor;
QImage image;
int zoom;
};

#endif

iconeditor.cpp

#include <QtGui>

#include "iconeditor.h"

IconEditor::IconEditor(QWidget *parent)
: QWidget(parent)
{
...
}

void IconEditor::setPenColor(const QColor &newColor)
{
curColor = newColor;
}

void IconEditor::setZoomFactor(int newZoom)
{
...
}

void IconEditor::setIconImage(const QImage &newImage)
{
...
}

QSize IconEditor::sizeHint() const
{
...
return size;
}

void IconEditor::mousePressEvent(QMouseEvent *event)
{
...
}

void IconEditor::mouseMoveEvent(QMouseEvent *event)
{
...
}

void IconEditor::paintEvent(QPaintEvent *event)
{
QPainter painter(this);

...
}

void IconEditor::setImagePixel(const QPoint &pos, bool opaque)
{
... not relevant
}

QRect IconEditor::pixelRect(int i, int j) const
{
...
}


iconeditorplugin.pro

TEMPLATE = lib
CONFIG += designer plugin release
HEADERS = ../iconeditor/iconeditor.h \
iconeditorplugin.h
SOURCES = ../iconeditor/iconeditor.cpp \
iconeditorplugin.cpp
RESOURCES = iconeditorplugin.qrc

DESTDIR = $$[QT_INSTALL_PLUGINS]/designer
target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target


And resources are okay in a qrc file. Why the plugin still can't load?

wysota
6th September 2010, 07:47
Run Designer and choose Help/About plugins from the menu. You'll see a reason why your plugin is failing.

eg3gg
6th September 2010, 07:59
Maybe I'm looking at the wrong window but I can't see other plugin options
http://img59.imageshack.us/img59/1055/eqwewq.th.jpg (http://img59.imageshack.us/i/eqwewq.jpg/)

Uploaded with ImageShack.us (http://imageshack.us)

SixDegrees
6th September 2010, 08:35
Your plugin isn't showing up at all. This indicates that designer doesn't see it. It's probably in the wrong directory. If you installed a new or upgraded version of Qt recently, you may have two seperate installations or partial installations and plugins are being installed in the wrong one.

If your plugin was found but failed to load properly, the window would list reasons for the failure. If it doesn't show up at all, it isn't being found.

eg3gg
6th September 2010, 08:56
You are right, there was another "designer" directory.

Now the error is "The plugin .../iconeditorplugin.dll uses incompatible Qt library. Expected build key "Windows msvc release full-config", got "Windows mingw release full-config"

Does this mean I need to compile that necessarily with visual studio?

tbscope
6th September 2010, 09:04
Yes, you need to compile the plugin with the same compiler as the host program.
This has to do with symbolic names etc... in the compiled binaries.

In other words, mingw and msvc speak different languages.

wysota
6th September 2010, 09:06
Yes. Or you can use Designer (and Qt) built with MinGW.

Please don't use 3rd party sites for storing images. Use the forum attachment feature instead.