Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unresolved external symbol when launching Qt's example
Qt 6.11 is out! See what's new in the release blog

Unresolved external symbol when launching Qt's example

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 534 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by Please_Help_me_D
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Please_Help_me_DP 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @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) ...

        Please_Help_me_DP Offline
        Please_Help_me_DP Offline
        Please_Help_me_D
        wrote on last edited by
        #3

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

        Christian EhrlicherC 1 Reply Last reply
        0
        • Please_Help_me_DP Please_Help_me_D

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

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Please_Help_me_D The code is there in the documentation.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          Please_Help_me_DP 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @Please_Help_me_D The code is there in the documentation.

            Please_Help_me_DP Offline
            Please_Help_me_DP Offline
            Please_Help_me_D
            wrote on last edited by
            #5

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

            void setupModelData(const QStringList &lines, TreeItem *parent);
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Please_Help_me_DP 1 Reply Last reply
              1
              • SGaistS SGaist

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

                Please_Help_me_DP Offline
                Please_Help_me_DP Offline
                Please_Help_me_D
                wrote on last edited by
                #7

                @SGaist thank you a lot :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved