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

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

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

您必须在使用前定义结构。所以放在class.

开头

此外,如果您有一个使用此类型的 public 方法,您也应该将其设为 public。

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

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