dobra, dlaczego o to pytam ?
Bo analizuję sobie przykład modelu w Qt i jest tam taka ciekawostka którą próbuję zrozumieć i dlatego założyłem ten temat
plik treemodel.cpp
TreeModel::TreeModel(const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
rootItem = new TreeItem({tr("Title"), tr("Summary")});
qDebug()<< "TreeModel rootItem" << rootItem;
setupModelData(data.split('\n'), rootItem); //analiza tej funkcji doprowadziła mnie do tego
//czemu założyłem ten wątek i tutaj przekazywany jest wskaźnik JAKBY "do samego siebie"
}
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) //tutaj przyjmuję obiekt typu TreeItem
{
QList<TreeItem *> parents;
QList<int> indentations;
parents << parent; //tutaj przypisuje się ten sam wskaźnik, a w zasadzie jego "rodzic"
indentations << 0;
int number = 0;
while (number < lines.count()) {
int position = 0;
while (position < lines[number].length()) {
if (lines[number].at(position) != ' ')
break;
position++;
}
const QString lineData = lines[number].mid(position).trimmed();
if (!lineData.isEmpty()) {
// Read the column data from the rest of the line.
const QStringList columnStrings =
lineData.split(QLatin1Char('\t'), Qt::SkipEmptyParts);
QList<QVariant> columnData;
columnData.reserve(columnStrings.count());
for (const QString &columnString : columnStrings)
columnData << columnString;
if (position > indentations.last()) {
// The last child of the current parent is now the new parent
// unless the current parent has no children.
if (parents.last()->childCount() > 0) {
parents << parents.last()->child(parents.last()->childCount()-1);
indentations << position;
}
} else {
while (position < indentations.last() && parents.count() > 0) {
parents.pop_back();
indentations.pop_back();
}
}
qDebug()<< "parents last" << parents.last();
// Append a new item to the current parent's list of children.
parents.last()->appendChild(new TreeItem(columnData, parents.last())); //drugi parametr konstrukotra i ten sam wskaźnik jest przekazywany
}
++number;
}
}
i dochodzimy do konstruktora obiektu TreeItem
plik treeitem.cpp
TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
: m_itemData(data), m_parentItem(parent) //wskaźnik m_parentItem który został przekazany w powyższym kodzie
{}
a klasa wygląda tak
plik treeitem.h
class TreeItem
{
public:
TreeItem *parentItem();
private:
QList<TreeItem *> m_childItems;
QList<QVariant> m_itemData;
TreeItem *m_parentItem; //i właśnie o te cudo mi chodzi i dlatego założyłem wątek
};
I dlatego zrobiłem sobie taki "pseudokod" aby zrozumieć co tu się dzieje i dlaczego w tym przykładzie zastosowali taki trik