[SOLVED]C++ class with QML



  • Hello i ve recently made a demo class to test if i can declare its object in main:
    Butit gives me unresolved external error, here is the header file:
    @
    #ifndef DETECTSQUARES_H
    #define DETECTSQUARES_H

    #include <QObject>

    class DetectSquares: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
    public:
    explicit DetectSquares(QObject *parent=0);
    ~DetectSquares();
    };

    #endif // DETECTSQUARES_H
    @

    I get error when i declare
    DetectSquares r;

    [Edited - Please use code tags "@@" - p3c0]


  • Moderators

    Hi,

    It seems you have not defined the functions for the property author. Just right click on Q_PROPERTY and click "Generate missiin Q_PROPERTY members".



  • @@ class DetectSquares: public QObject
    {
    Q_OBJECT
    public:
    void Draw();
    }; @@

    I made it simple but if i call it in the main function like this :
    @@ DetectSquares * r();
    r.Draw(); @@

    I get error .Draw must have class/struct/union


  • Moderators

    Since you are decalring it a pointer it should be
    @
    DetectSquares *r;
    r->Draw();
    @

    or keep it on stack
    @
    DetectSquares r;
    r.Draw();
    @



  • If you choose to use a pointer do not forget to create the object.
    Even if that compiles you will get a crash because r does not point to a valid object.
    Even better use an automatic pointer to avoid memory leaks:
    @QScopedPointer<DetectSquares> r(new DetectSquares());
    r->Draw();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.