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

    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

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

    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.

  3. #3
    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.

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

    Default Re: Custom Widget

    Also debugger doesn´t seem to work that well in Mac os X 10.6.2 It ignores my breakpoints

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

    Default Re: Custom Widget

    LIBS += -llibspeeddialplugin
    AND
    LIBS += -L/Developer/Applications/Qt/plugins/designer/
    ==========================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

    Very strange. I have tried all types of LIBS += statements but when i try to include speeddial.h or speeddialplugin.h whichever thats supposed to be used to be able to create an instance of my widget it still complains that there is no such type. Doesn´t seem like my speeddialplugin.dylib is working at all.

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

    Default Re: Custom Widget

    By linking to my lib file I should be able to create an instance of my custom widget manually? And if i can´t there has to be something wrong with the plugin? Since it doesn´t recognize the type of my custom plugin.

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

    Default Re: Custom Widget

    By linking to my lib file I should be able to create an instance of my custom widget manually?
    Yes.
    And if i can´t there has to be something wrong with the plugin?
    Depends what "can't" means.
    It is well possible that you will be able to create an instance but it will crash.

    But then at least you can follow the code and see exactly what and where the problem is.
    ==========================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.

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

    Default Re: Custom Widget

    But i cant create an instance of my custom widget since the compiler doesn´t recognize the type when I try to include speedDial.h

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

    Default Re: Custom Widget

    post your speedDial.h (please, not in as a link!)
    ==========================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

    My custom widget is actually just the svgDialgauge wich is just included in my speedDialPlugin project as a .pri. So my speedDial.h would be qtSvgDialGauge.h I have ofcourse tried including qtsvgdialgauge.h and speedDialPlugin.h but the compiler doesnt recohnize these as well even when im linking to the lib file as we talked about earlier. The qtsvgdialgauge.h is located in a sub folder of my pluginprojectfolder.

    So my plugin project consists of

    speedDialPlugin.h
    speedDialPlugin.cpp
    (Included by .pri files)
    qtSvgDialGauge.h
    qtSvgDialGauge.cpp
    qtSvgPixMapCache.h
    qtSvgPixMapCache.cpp

    qtSvgPixMapCache is used by svgDialGauge. And svgDialGauge is the widget created in the createWidget() function in speedDialPlugin.cpp


    /*
    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
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Custom Widget

    now let me understand:
    when you include the above file, and do:
    Qt Code:
    1. QtSvgDialGauge *pDialGauge = new QtSvgDialGauge(this);
    To copy to clipboard, switch view to plain text mode 

    you get an undefined type?

    Where as, this is also not exactly what I meant.
    You should include your plugin header, and link directly to your plugin, in order to test it.
    So in your case it will be something like:
    Qt Code:
    1. speedDialPlugin *pPlugIn = new speedDialPlugin();
    2. QtSvgDialGauge *pDialGauge = pPlugIn->createWidget();
    To copy to clipboard, switch view to plain text mode 

    Your project needs to know the include paths to all the classes that you will be using.
    Last edited by high_flyer; 12th February 2010 at 15:38.
    ==========================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.

Similar Threads

  1. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  2. eventFilter: pop-up custom widget
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 22nd November 2007, 09: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, 15:38
  4. Replies: 1
    Last Post: 5th November 2006, 23:50
  5. Replies: 9
    Last Post: 8th May 2006, 14: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
  •  
Qt is a trademark of The Qt Company.