在假設的例子中,我有一個C++組件 -如何實例下,在QML ++組件 - 帶參數的構造函數
class Board : public QObject
{
Q_OBJECT
public:
//Q_PROPERTYs go here
explicit Board(int rows, int columns)
{
matrix = std::vector<int>(rows, std::vector<int>(columns, 0));
}
~Board()
{
matrix.clear();
}
Q_INVOKABLE void checkAndUpdateAdjecentCells(int row, int column);
//setters and getters here.
signals:
void matrixUpdated();
private:
Board(QObject *parent) = default; //i intend to do this.
Board(Board& b) = delete;
int nRows_, nCols_;
std::vector<std::vector> matrix;
};
在main()
註冊一樣 -
qmlRegisterType<Board>("SameGameBackend", 1, 0, "BubbleBoard");
問題
我如何在QML中實例化這個參數化構造函數?
預期QML代碼 -
BubbleBoard{
id: bboard
rows: 10
columns: 10
}
我們可以擴展這個問題,包括初始化列表。 只好nRows_
和nCols_
一直const int
, 構造本來
explicit Board(int rows, int columns):nRows_(rows), nCols_(columns){}
是否有可能從內部QML實例這樣的組件?
AFAIK,沒有這樣的功能。請參閱下面的答案。 – Tarod