Results 1 to 20 of 28

Thread: Custom Widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question Custom Widget

    Hi

    I have created a custom widget and by creating a plugin succesfully added it to the widgetbox in designer. However when I try to use the slots my custom widget has, Designer crashes. Also when I press preview my widget doesnt show, but it is working in designer. Any idea how to find out what is going wrong? I would also like to use my custom widgets directly in creator is this possible? When trying to use the forms that I have created, in Designer with my widgets, in Creator i get alot of compile error, like it doent know the type of my plugin object and so on.. So there has to be a way to integrate this in creator?
    BTW im using mac

    //Frej

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget

    However when I try to use the slots my custom widget has, Designer crashes.
    Try using the custom widget in a normal project, as part of the project and not as plugin in designer.
    Does is crash when you use the slots?

    If it does, the problem is in your widget implementation.
    If it does not, show us your plugin wrapper class.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    It works fine using the slots normally.
    My plugin class looks like this:

    #ifndef SPEEDDIALPLUGIN_H
    #define SPEEDDIALPLUGIN_H

    #include <QDesignerCustomWidgetInterface>

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

    public:
    speedDialPlugin(QObject *parent = 0);

    bool isContainer() const;
    bool isInitialized() const;
    QIcon icon() const;
    QString domXml() const;
    QString group() const;
    QString includeFile() const;
    QString name() const;
    QString toolTip() const;
    QString whatsThis() const;
    QWidget *createWidget(QWidget *parent);
    void initialize(QDesignerFormEditorInterface *core);

    private:
    bool m_initialized;
    };

    #endif

    This code was generated when using the "Create plugin template". I then just altered the following code to create my costum widget.

    QWidget *speedDialPlugin::createWidget(QWidget *parent)
    {
    //return new speedDial(parent);
    QtSvgDialGauge * gauge = new QtSvgDialGauge(parent);
    gauge->setSkin("Tachometer");
    //gauge->setNeedleOrigin(0.486, 0.466);
    gauge->setMinimum(0);
    gauge->setMaximum(60);
    //gauge->setStartAngle(-130);
    //gauge->setEndAngle(133);
    gauge->setValue(0);
    QFont sansFont( "Helvetica [Cronyx]", 12 );
    gauge->setFont(sansFont);
    //gauge->setMaximumSize(64, 64);
    //gauge->show();
    return gauge;
    }

  4. #4
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    Hi I really need to get my plugin working in creator so please come with suggestions

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget

    Also when I press preview my widget doesn't show, but it is working in designer.
    Can you explain a bit more what do you mean by that?
    How do you know it works, if you can't see it?
    Do you mean you see only an empty widget on the form?
    If so, it suggests that only a QWidget is being initialized, and not an instance of your custom widget.
    Does the ui generated code includes your custom widget header file? (or does includeFile() return the correct include file?)

    In such cases its very hard to say what the problem is, with out seeing the whole code both of the plugin, and the custom widget.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    I can drag my custom widget from the widgetbox in designer and it shows up in the form. When I hit preview it doesn´t show anymore and when I try to use its slots designer craches.. I think your on to something. When I import the ui file from designer to a project in creatior and compile it then complains that there is no such type (as my custom widget). So there might be something wrong includeFile(). But why is it showing up in designer when I use the widget there in that case? My custum widget is actually just the svgdialgauge.pri widget that qt has provided as an example and I just used the custom wifget plugin templete in creator to create the plugin so it should work. Isn´t there a way to get custom widgets in the form builder in creator so I don´t have to go via Designer.

    Im very grateful for your help

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget

    When I import the ui file from designer to a project in creatior and compile it then complains that there is no such type
    This makes my suspicion even stronger.
    To make sure this is the problem:
    From designer, try saving an ui file with your custom widget/plugin.
    Compile only the ui, and have a look at the cpp generated,check to see if it has the correct include file there.
    If it doesn't then your includeFile() is not correctly implemented.

    Can you show your implementation of includeFile()?

    But why is it showing up in designer when I use the widget there in that case?
    Because in your plugin project you did include your custom widget header.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    QString speedDialPlugin::includeFile() const
    {
    return QLatin1String("qtsvgdialgauge.h");
    }

    Im not really sure what you mean by looking at generated cpp file, I guess you mean .h file? I used uic command to look at genreated .h file of my .ui with my widget in it. At the top #include "qtsvgdialgauge.h", but of course depending on where the ui file is located it doesnt know where the qtsvgdialgauge.h file is.

  9. #9
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    Maybe, the reason why my widget isn´t showing in preview is that my custom widget is using a help class and that class should ofcourse also be added in the includeFile() method?

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget

    I guess you mean .h file?
    Yes sorry.

    but of course depending on where the ui file is located it doesnt know where the qtsvgdialgauge.h file is.
    This is not the problem, if your project doesn't know where the header is, you will get a compiler error, which I understand you are not getting.

    Ok, the next thing to try is not to use the designer, but make an instance in a widget manually, and simply link against the lib file and the DLL plugin.
    In this test application connect to a slot of your custom widget, and follow the execution in a debugger, then you will see exactly where and what the problem is.

    Again - don't include the custom widget code in your test application, just use the header file, and link against the plugin.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    This is my code

    speedDial.pro
    CONFIG += designer \
    plugin \
    debug_and_release
    TARGET = $$qtLibraryTarget(speeddialplugin)
    TEMPLATE = lib
    HEADERS = speeddialplugin.h

    SOURCES = speeddialplugin.cpp

    RESOURCES = icons.qrc
    LIBS += -L.
    target.path = $$[QT_INSTALL_PLUGINS]/designer
    INSTALLS += target

    include(svgdialgauge/svgdialgauge.pri)
    include(common/common.pri)
    FORMS += form.ui


    speedDialPlugin.h


    #ifndef SPEEDDIALPLUGIN_H
    #define SPEEDDIALPLUGIN_H

    #include <QDesignerCustomWidgetInterface>

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

    public:
    speedDialPlugin(QObject *parent = 0);

    bool isContainer() const;
    bool isInitialized() const;
    QIcon icon() const;
    QString domXml() const;
    QString group() const;
    QString includeFile() const;
    QString name() const;
    QString toolTip() const;
    QString whatsThis() const;
    QWidget *createWidget(QWidget *parent);
    void initialize(QDesignerFormEditorInterface *core);

    private:
    bool m_initialized;
    };
    #endif


    speedDialPlugin.cpp


    #include <QtCore/QtPlugin>
    #include "speeddialplugin.h"
    #include "qtsvgdialgauge.h"

    speedDialPlugin::speedDialPlugin(QObject *parent)
    : QObject(parent)
    {
    m_initialized = false;
    }

    void speedDialPlugin::initialize(QDesignerFormEditorInterface * /* core */)
    {
    if (m_initialized)
    return;

    // Add extension registrations, etc. here

    m_initialized = true;
    }

    bool speedDialPlugin::isInitialized() const
    {
    return m_initialized;
    }

    QWidget *speedDialPlugin::createWidget(QWidget *parent)
    {
    QtSvgDialGauge * gauge = new QtSvgDialGauge(parent);
    gauge->setSkin("Tachometer");
    gauge->setMinimum(0);
    gauge->setMaximum(60);
    gauge->setValue(0);
    return gauge;
    }

    QString speedDialPlugin::name() const
    {
    return QLatin1String("speedDial");
    }

    QString speedDialPlugin::group() const
    {
    return QLatin1String("");
    }

    QIcon speedDialPlugin::icon() const
    {
    return QIcon();
    }

    QString speedDialPlugin::toolTip() const
    {
    return QLatin1String("");
    }

    QString speedDialPlugin::whatsThis() const
    {
    return QLatin1String("");
    }

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

    QString speedDialPlugin::domXml() const
    {
    //return QLatin1String("<widget class=\"speedDial\" name=\"speedDial\">\n</widget>\n");
    return "<ui language=\"c++\">\n"
    " <widget class=\"speedDial\" name=\"speedDial\">\n"
    " <property name=\"geometry\">\n"
    " <rect>\n"
    " <x>0</x>\n"
    " <y>0</y>\n"
    " <width>100</width>\n"
    " <height>100</height>\n"
    " </rect>\n"
    " </property>\n"
    " </widget>\n"
    "</ui>";
    }

    QString speedDialPlugin::includeFile() const
    {
    return QLatin1String("qtsvgdialgauge.h");
    }

    Q_EXPORT_PLUGIN2(speeddialplugin, speedDialPlugin)

    qtSvgDialGauge.h

    /*
    Embedded Widgets Demo
    Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).*
    Contact: Qt Software Information (qt-info@nokia.com)**
    This file may be used under the terms of the Embedded Widgets Demo License
    Agreement.
    */

    #ifndef QT_SVG_DIAL_GAUGE
    #define QT_SVG_DIAL_GAUGE
    #include <QtGui/QWidget>
    #include <QtCore/QPair>
    #include <QtDesigner/QDesignerExportWidget>



    class QSvgRenderer;
    class QtSvgPixmapCache;

    class QDESIGNER_WIDGET_EXPORT QtSvgDialGauge : public QWidget
    {
    Q_OBJECT
    Q_PROPERTY(QString skin READ skin WRITE setSkin)
    Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
    Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
    Q_PROPERTY(qreal startAngle READ startAngle WRITE setStartAngle)
    Q_PROPERTY(qreal endAngle READ endAngle WRITE setEndAngle)
    public:
    explicit QtSvgDialGauge(QWidget * parent = 0);
    ~QtSvgDialGauge();

    void setSkin(const QString& skin);
    QString skin() const;


    void setMinimum(int minimum);
    void setMaximum(int maximum);
    void setNeedleOrigin(qreal x, qreal y);
    void setStartAngle(qreal angle);
    void setEndAngle(qreal angle);

    int value() const;
    int minimum() const;
    int maximum() const;
    qreal needleOriginX() const;
    qreal needleOriginY() const;
    qreal startAngle() const;
    qreal endAngle() const;

    virtual QSize minimumSizeHint() const;
    virtual QSize sizeHint() const;
    void setShowOverlay(bool);

    public slots:
    void setValue(int value);

    private:
    void init();
    QRectF availableRect(QtSvgPixmapCache * renderObject) const;

    QtSvgPixmapCache* m_backgroundRenderer;
    QtSvgPixmapCache* m_needleShadowRenderer;
    QSvgRenderer* m_needleRenderer;
    QRectF availableRect(QSvgRenderer * renderObject) const;
    QtSvgPixmapCache* m_overlayRenderer;
    /** minimum possible value **/
    int m_minimum;
    /** maximum possible value **/
    int m_maximum;
    /** actual value **/
    int m_value;
    /** smallest start angle **/
    qreal m_startAngle;
    /** highest end angle **/
    qreal m_endAngle;
    /** position x of needle **/
    qreal m_originX;
    /** position y of needle **/
    qreal m_originY;
    bool m_showOverlay;

    /** name of actual skin **/
    QString m_skin;
    protected:
    void paintEvent(QPaintEvent * event);
    };

    #endif // QT_SVG_DIAL_GAUGE

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widget

    1. please include the code in the code tags, and not as link.
    2. You posted the header of your custom widget, it tells little, the implementation is the important stuff.
    3. I am not a compiler, and its hard to make out mistakes that might bring your app to crash - such as pointer management if you can't even suggest where the problem might be.

    the best way is to do what I just offered in my previous post - run the the whole thing in a debugger, and you will get exactly the place where it crashes.
    When you have that, and still need help, ask again.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Feb 2010
    Posts
    30
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom Widget

    I see. When linking to a static library i should just in my .pro file write:
    LIBS += libspeeddialplugin.lib
    or
    LIBS += /Developer/Applications/Qt/plugins/designer/libspeeddialplugin.lib

    But this doen´t seem to work.

Similar Threads

  1. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 15:06
  2. eventFilter: pop-up custom widget
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 22nd November 2007, 10:54
  3. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 16:38
  4. Replies: 1
    Last Post: 6th November 2006, 00:50
  5. Replies: 9
    Last Post: 8th May 2006, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.