简体   繁体   中英

How to pass a struct declared in a Qt's class to the same class's function?

#ifndef FF_H
#define FF_H

#include <QtQuick/QQuickPaintedItem>
#include "qpainter.h"
#include <QQuickItem>
#include <QQuickWindow>

class Draw_on_qimage : public QQuickPaintedItem
{
    Q_OBJECT
public:
    virtual void paint(QPainter *);
    Draw_on_qimage(QQuickPaintedItem *parent = 0);

public slots:
    void setbb_list(QList<bounding_box_struct> bb_list)
    {
        for (int h = 0; h < bb_list.size(); h++)
        {
            bounding_box_struct obj;
            obj.x = bb_list[h].x;

            m_bb_list.push_back(obj);
        }

        emit bb_listChanged(m_bb_list);
    }

protected:
    virtual void componentComplete ();

private:
    struct bounding_box_struct
    {   int x;
        int y;
        int w;
        int h;
        std::string vehicle_type;
    };

    Q_PROPERTY(QList<bounding_box_struct> bb_list
               READ bb_list
               WRITE setbb_list
               NOTIFY bb_listChanged)

    QList<bounding_box_struct> m_bb_list;

signals:
    void bb_listChanged(QList<bounding_box_struct> bb_list);
};

#endif // FF_H

The bb_list has to be a Q_PROPERTY .

I am getting following errors:

error: ‘bounding_box_struct’ was not declared in this scope
     void setbb_list(QList<bounding_box_struct> bb_list)
                           ^
error: request for member ‘size’ in ‘bb_list’, which is of non-class type ‘int’
         for (int h = 0; h < bb_list.size(); h++)
                                     ^

error: invalid types ‘int[int]’ for array subscript
             obj.x = bb_list[h].x;
                              ^

You must define the struct before you use it. So put it in the beginning of the class.

Additionally, if you have a public method that uses this type, you should make it public, too.

Also note that if you put your method implementation outside of the class definition, you have to fully qualify the struct in return types, but it's optional in parameters:

struct MyClass {
    struct Inner {};
    Inner doThis(Inner i);
    Inner doThat(Inner i) { return i; } // Not qualified
};

MyClass::Inner MyClass::doThis(Inner i) { return i; }
// ^ required                   ^ Can be qualified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM