如何将在Qt类中声明的结构传递给同一个类的函数?

时间:2017-12-07 10:29:49

标签: c++ qt struct qtquick2

#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

bb_list必须是Q_PROPERTY

我收到以下错误:

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;
                              ^

1 个答案:

答案 0 :(得分:3)

您必须在使用之前定义结构。所以把它放在课程的开头。

此外,如果您有一个使用此类型的公共方法,您也应该公开它。

另请注意,如果将方法实现放在类定义之外,则必须在返回类型中完全限定struct,但在参数中它是可选的:

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