Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unresolved external symbol when launching Qt's example



  • Hi,

    I'm trying to run example from Qt documentation about QTreeView.
    I copied two classes:
    treeitem.h

    #ifndef TREEITEM_H
    #define TREEITEM_H
    
    #include <QtCore>
    
    class TreeItem
    {
    public:
        explicit TreeItem(const QVector<QVariant> &data, TreeItem *parentItem = nullptr);
        ~TreeItem();
    
        void appendChild(TreeItem *child);
    
        TreeItem *child(int row);
        int childCount() const;
        int columnCount() const;
        QVariant data(int column) const;
        int row() const;
        TreeItem *parentItem();
    
    private:
        QVector<TreeItem*> m_childItems;
        QVector<QVariant> m_itemData;
        TreeItem *m_parentItem;
    };
    
    #endif // TREEITEM_H
    
    

    treeitem.cpp

    #include "treeitem.h"
    
    TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
        : m_itemData(data), m_parentItem(parent)
    {
    
    }
    
    TreeItem::~TreeItem()
    {
        qDeleteAll(m_childItems);
    }
    
    void TreeItem::appendChild(TreeItem *item)
    {
        m_childItems.append(item);
    }
    
    TreeItem *TreeItem::child(int row)
    {
        if (row < 0 || row >= m_childItems.size())
            return nullptr;
        return m_childItems.at(row);
    }
    
    int TreeItem::childCount() const
    {
        return m_childItems.count();
    }
    
    int TreeItem::row() const
    {
        if (m_parentItem)
            return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));
    
        return 0;
    }
    

    And the seond class:
    treemodel.h

    #ifndef TREEMODEL_H
    #define TREEMODEL_H
    
    #include <QtCore>
    #include <treeitem.h>
    
    
    class TreeModel : public QAbstractItemModel
    {
        Q_OBJECT
    
    public:
        explicit TreeModel(const QString &data, QObject *parent = nullptr);
        ~TreeModel();
    
        QVariant data(const QModelIndex &index, int role) const override;
        Qt::ItemFlags flags(const QModelIndex &index) const override;
        QVariant headerData(int section, Qt::Orientation orientation,
                            int role = Qt::DisplayRole) const override;
        QModelIndex index(int row, int column,
                          const QModelIndex &parent = QModelIndex()) const override;
        QModelIndex parent(const QModelIndex &index) const override;
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    
    private:
        void setupModelData(const QStringList &lines, TreeItem *parent);
    
        TreeItem *rootItem;
    };
    
    #endif // TREEMODEL_H
    
    

    treemodel.cpp

    #include "treemodel.h"
    
    TreeModel::TreeModel(const QString &data, QObject *parent)
        : QAbstractItemModel(parent)
    {
        rootItem = new TreeItem({tr("Title"), tr("Summary")});
        setupModelData(data.split('\n'), rootItem);
    }
    
    TreeModel::~TreeModel()
    {
        delete rootItem;
    }
    
    QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
    {
        if (!hasIndex(row, column, parent))
            return QModelIndex();
    
        TreeItem *parentItem;
    
        if (!parent.isValid())
            parentItem = rootItem;
        else
            parentItem = static_cast<TreeItem*>(parent.internalPointer());
    
        TreeItem *childItem = parentItem->child(row);
        if (childItem)
            return createIndex(row, column, childItem);
        return QModelIndex();
    }
    
    QModelIndex TreeModel::parent(const QModelIndex &index) const
    {
        if (!index.isValid())
            return QModelIndex();
    
        TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
        TreeItem *parentItem = childItem->parentItem();
    
        if (parentItem == rootItem)
            return QModelIndex();
    
        return createIndex(parentItem->row(), 0, parentItem);
    }
    
    int TreeModel::rowCount(const QModelIndex &parent) const
    {
        TreeItem *parentItem;
        if (parent.column() > 0)
            return 0;
    
        if (!parent.isValid())
            parentItem = rootItem;
        else
            parentItem = static_cast<TreeItem*>(parent.internalPointer());
    
        return parentItem->childCount();
    }
    
    int TreeModel::columnCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
        return rootItem->columnCount();
    }
    
    QVariant TreeModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role != Qt::DisplayRole)
            return QVariant();
    
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
    
        return item->data(index.column());
    }
    
    Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    {
        if (!index.isValid())
            return Qt::NoItemFlags;
    
        return QAbstractItemModel::flags(index);
    }
    
    QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
                                   int role) const
    {
        if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
            return rootItem->data(section);
    
        return QVariant();
    }
    

    If I add those file to the Qt project then I get the following errors when the app is building:
    1.png

    How to launch this example?


  • Qt Champions 2019

    @Please_Help_me_D said in Unresolved external symbol when launching Qt's example:

    How to launch this example?

    By simple don't forget to implement those functions (or copy them over) ...


  • Qt Champions 2019

    @Please_Help_me_D said in Unresolved external symbol when launching Qt's example:

    How to launch this example?

    By simple don't forget to implement those functions (or copy them over) ...



  • @Christian-Ehrlicher thank you! that helped
    The only thing that left to do is to understand how to implement TreeModel::setupModelData()


  • Qt Champions 2019

    @Please_Help_me_D The code is there in the documentation.



  • @Christian-Ehrlicher really? I can only see the declaration inside class TreeModel:

    void setupModelData(const QStringList &lines, TreeItem *parent);
    

  • Lifetime Qt Champion

    The complete code is linked to at the bottom of the page of the documentation.



  • @SGaist thank you a lot :)


Log in to reply