Unsolved Qt Designer cannot find my custom widget
-
Hello there, so I have created a widget and compiled it, everything went well but I can't find it in Qt Designer.
I've put it in multiple places but no luck
/home/chajussaib/Qt/5.5/gcc_64/plugins/designer/libiconEditorPlugin.so /home/chajussaib/Qt/Tools/QtCreator/bin/plugins/designer/libiconEditorPlugin.so /usr/lib/x86_64-linux-gnu/qt5/plugins/designer/libiconEditorPlugin.so /usr/lib/i386-linux-gnu/qt4/plugins/designer/libiconEditorPlugin.so
I launch Qt Creator like this
sh /home/chajussaib/Qt/Tools/QtCreator/bin/qtcreator.sh
I've tried
QT_PLUGIN_PATH=/usr/lib/x86_64-linux-gnu/qt5/plugins/ /home/chajussaib/Qt/Tools/QtCreator/bin/qtcreator.sh
Thank you
EDIT:
Here is the code:
./iconEditor/iconEditor.h#ifndef ICONEDITOR_H #define ICONEDITOR_H #include <QColor> #include <QImage> #include <QWidget> class 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) 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 opague); QRect pixelRect(int i, int j) const; QColor curColor; QImage image; int zoom; }; #endif // ICONEDITOR_H
./iconEditor/iconEditor.cpp
#include "iconEditor.h" #include <QtWidgets> IconEditor::IconEditor(QWidget *parent) : QWidget(parent), curColor(Qt::black), image(QImage(16, 16, QImage::Format_ARGB32)), zoom(8) { setAttribute(Qt::WA_StaticContents); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); image.fill(qRgba(0, 0, 0, 0)); } QSize IconEditor::sizeHint() const { QSize size = zoom * image.size(); if (zoom >= 3) size += QSize(1, 1); return size; } void IconEditor::setPenColor(const QColor &newColor) { curColor = newColor; } void IconEditor::setIconImage(const QImage &newImage) { if (newImage != image) { image = newImage.convertToFormat(QImage::Format_ARGB32); update(); updateGeometry(); } } void IconEditor::setZoomFactor(int newZoom) { if (newZoom < 1) newZoom = 1; if (newZoom != zoom) { zoom = newZoom; update(); updateGeometry(); } } void IconEditor::paintEvent(QPaintEvent *event) { QPainter painter(this); if (zoom >= 3) { painter.setPen(palette().foreground().color()); for (int i = 0; i <= image.width(); ++i) painter.drawLine(zoom * i, 0, zoom * i, zoom * image.height()); for (int j = 0; j <= image.height(); ++j) painter.drawLine(0, zoom * j, zoom * image.width(), zoom * j); } for (int i = 0; i < image.width(); ++i) { for (int j = 0; j < image.height(); ++j) { QRect rect = pixelRect(i, j); if (event->region().intersects(rect)) { QColor color = QColor::fromRgba(image.pixel(i, j)); if (color.alpha() < 255) painter.fillRect(rect, Qt::white); painter.fillRect(rect, color); } } } } QRect IconEditor::pixelRect(int i, int j) const { if (zoom >= 3) return QRect(zoom * i + 1, zoom * j + 1, zoom - 1, zoom - 1); else return QRect(zoom * i, zoom * j, zoom, zoom); } void IconEditor::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) setImagePixel(event->pos(), true); else if (event->button() == Qt::RightButton) setImagePixel(event->pos(), false); } void IconEditor::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) setImagePixel(event->pos(), true); else if (event->buttons() & Qt::RightButton) setImagePixel(event->pos(), false); } void IconEditor::setImagePixel(const QPoint &pos, bool opaque) { int i = pos.x() / zoom; int j = pos.y() / zoom; if (image.rect().contains(i, j)) { if (opaque) image.setPixel(i, j, penColor().rgba()); else image.setPixel(i, j, qRgba(0, 0, 0, 0)); update(pixelRect(i, j)); } }
./iconEditorPlugin/iconEditorPlugin.h
#ifndef ICONEDITORPLUGIN_H #define ICONEDITORPLUGIN_H #include <QtUiPlugin/QDesignerCustomWidgetInterface> class IconEditorPlugin : public QObject, public QDesignerCustomWidgetInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "basictools.json") Q_INTERFACES(QDesignerCustomWidgetInterface) public: IconEditorPlugin(QObject *parent = 0) : QObject(parent) { } 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 // ICONEDITORPLUGIN_H
./iconEditorPlugin/iconEditorPlugin.cpp
#include "../iconEditor/iconEditor.h" #include "iconEditorPlugin.h" #include <QtPlugin> 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 is a simple icon editor widget."); } bool IconEditorPlugin::isContainer() const { return false; } QWidget *IconEditorPlugin::createWidget(QWidget *parent) { return new IconEditor(parent); }
./iconEditorPlugin/iconEditorPlugin.pro
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets designer lessThan(QT_MAJOR_VERSION, 5): CONFIG += designer CONFIG += plugin release TEMPLATE = lib TARGET = $$qtLibraryTarget($$TARGET) target.path = $$[QT_INSTALL_PLUGINS]/designer INSTALLS += target DESTDIR = $$[QT_INSTALL_PLUGINS]/designer Iinput HEADER = ../iconEditor/iconEditor.h \ iconEditorPlugin.h SOURCES = ../iconEditor/iconeditor.cpp \ iconEditorPlugin.cpp #Resources RESOURCES = iconEditorPlugin.qrc
-
Hi,
You can set the QT_DEBUG_PLUGINS environment variable to one before starting Qt Creator to have more information about what is happening.
By the way, you need to use the same version of Qt as the one used to build Qt Creator. Did you check that they are matching ?
-
I ran this
QT_PLUGIN_PATH=/home/chajussaib/Qt/Tools/QtCreator/bin/plugins QT_DEBUG_PLUGINS=1 /home/chajussaib/Qt/Tools/QtCreator/bin/qtcreator.sh
and got the following output, don't think it loaded libiconEditorPlugin.
I got this version of Qt: http://www.qt.io/download-open-source/
so I believe the versions are the same(5.5.1). -
@ChajusSaib In your plugin you're claiming to implement the
org.qt-project.Qt.QStyleFactoryInterface
interface. However, in yourQ_INTERFACES
macro you claim to implementQDesignerCustomWidgetInterface
.Honestly it has taken me quite some time to figure this out, it is not very well explained in the documentation, nor do I really understand why we need this duplication. However: the string-based interface you pass as argument to the
IID
parameter inQ_PLUGIN_METADATA
must match a previous call toQ_DECLARE_INTERFACE(InterfaceDefinition, IIDstring)
In other words: I strongly suspect your plugin will be loaded once you write:
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface" FILE "basictools.json")
-
@Jakob Thank you mate for the help but unfortunately still no luck. My .json file is empty by the way, is that the issue? I'm new to Qt so please excuse me. Thank you!
-
The question might be silly but since there's not mention of libiconEditorPlugin in your log, do you have it located in the right folder ?
-
@SGaist I think I'm the one being silly.
Let me explain everything I do, so I have two directories: iconEditor and iconEditorPlugin. I go to iconEditorPlugin and run qmake *.pro and sudo make.
This creates a .so file in my qt/plugins/designer folder. That's basically it, I launch Qt Creator to check if it's in the designer but it isn't.
I feel like I have missed out a very simple step that I have to add my code to my current project or something.
-
Why sudo ?
-
@SGaist Needs permission to move the .so file to Qt directory.
-
In your home folder ?
Are you sure you're not installing the plugin on some other part of the system ?
-
@SGaist Well qmake(command) uses /usr/lib/x86_64-linux-gnu/qt5/
While Qt Creator uses Qt in my home directory, I've placed the files in the following directories but no luck.
/home/chajussaib/Qt/5.5/gcc_64/plugins/designer/libiconEditorPlugin.so
/home/chajussaib/Qt/Tools/QtCreator/bin/plugins/designer/libiconEditorPlugin.so
/usr/lib/x86_64-linux-gnu/qt5/plugins/designer/libiconEditorPlugin.so
/usr/lib/i386-linux-gnu/qt4/plugins/designer/libiconEditorPlugin.so -
Check which version of Qt Qt Creator is using, you have to use the same (version and architecture)
Don't copy your plugin in your system's installed Qt.
-
@SGaist said:
Check which version of Qt Qt Creator is using, you have to use the same (version and architecture)
Don't copy your plugin in your system's installed Qt.
Qt Creator: ```
Based on Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit)
qmake: QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu/qt5/libI believe its the version of the compiler as my version is ``` gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4