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. Using Tree Model to Store Data?
Forum Updated to NodeBB v4.3 + New Features

Using Tree Model to Store Data?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 618 Views
  • 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.
  • A Offline
    A Offline
    AlexanderAlexander
    wrote on last edited by AlexanderAlexander
    #1

    Currently:
    I have created and populated a QTreeWidget with QTreeWidgetItems, and have set the text of the QTreeWidgetItems

    Problem: I have no idea how to actually utilize the QTreeWidgetItems for data storage
    Looking at: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

    void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
    

    I see that I can use a Qt::ItemDataRole to specify the type of data stored, but none of the options in the enumeration pertain to actually storing raw data, only Qt properties.

    Questions:

    • How would I store actual data, such as doubles, within a WTreeWidgetItem?

    • If I am not supposed to actually store data within the tree items, do I need to use an auxilary structure and use model indexes with internal pointers to link the 2 together?

    Info:

    • Qt5 (c++)

    • MSVC 2017 + Qt Designer[link text](link url)

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

      @AlexanderAlexander said in Using Tree Model to Store Data?:

      How would I store actual data, such as doubles, within a WTreeWidgetItem?

      You can store whatever you want in the QVariant.

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

      1 Reply Last reply
      2
      • A AlexanderAlexander

        Currently:
        I have created and populated a QTreeWidget with QTreeWidgetItems, and have set the text of the QTreeWidgetItems

        Problem: I have no idea how to actually utilize the QTreeWidgetItems for data storage
        Looking at: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

        void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
        

        I see that I can use a Qt::ItemDataRole to specify the type of data stored, but none of the options in the enumeration pertain to actually storing raw data, only Qt properties.

        Questions:

        • How would I store actual data, such as doubles, within a WTreeWidgetItem?

        • If I am not supposed to actually store data within the tree items, do I need to use an auxilary structure and use model indexes with internal pointers to link the 2 together?

        Info:

        • Qt5 (c++)

        • MSVC 2017 + Qt Designer[link text](link url)

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @AlexanderAlexander said in Using Tree Model to Store Data?:

        I can use a Qt::ItemDataRole to specify the type of data stored

        No. the Qt::ItemDataRole is just a "hint" about what you are storing. the type of data is completely determined by the variant. In all Qt native models Qt::DisplayRole and Qt::EditRole are exactly the same

        I have no idea how to actually utilize the QTreeWidgetItems for data storage

        Examples:

        • Set the text of the item in the first column: item->setData(0,Qt::DisplayRole,QStringLiteral("Hello"));
        • Set a numerical value as displayed in the first column: item->setData(0,Qt::DisplayRole,3.14);
        • Set the text colour for the the item in the first column: item->setData(0,Qt::ForegroundRole,QBrush(Qt::red));
        • Store a value that your program will use later: item->setData(0,Qt::UserRole,5);
        • Store another value that your program will use later: item->setData(0,Qt::UserRole+1,35);

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3

        • Login

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