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. How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?
Forum Updated to NodeBB v4.3 + New Features

How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?

Scheduled Pinned Locked Moved Unsolved General and Desktop
table widgettable model
6 Posts 5 Posters 830 Views 3 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.
  • J Offline
    J Offline
    jdent
    wrote on last edited by
    #1

    In MFC one could store data in a CListBox together with the text to be displayed so one could access this data (any object really) during program execution... I need to do the same in a QTableWidget... What is the most natural way? The data I want to store is the row contents of the widget... is it easy to create a simple model to encapsulate this vector of rows?

    Can someone point me on how to create this model?

    Thanks!!

    Juan

    Pl45m4P 1 Reply Last reply
    0
    • J jdent

      In MFC one could store data in a CListBox together with the text to be displayed so one could access this data (any object really) during program execution... I need to do the same in a QTableWidget... What is the most natural way? The data I want to store is the row contents of the widget... is it easy to create a simple model to encapsulate this vector of rows?

      Can someone point me on how to create this model?

      Thanks!!

      Juan

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @jdent said in How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?:

      I need to do the same in a QTableWidget

      That might be difficult. I think you have to use QTableView and implement the model + a proper delegate yourself.

      If I understand correctly, you want to store objects like CListBox (don't know what this exactly is) including the data they are currently displaying in a table?!
      Is CListBox more like QComboBox or more like QLineEdit?

      The whole approach seems weird. Wouldn't it be better to store the display data right away in the table model and then link the widgets to it somehow?!
      Why you want to store the widgets as "data" in your model?

      Can you give a example how your "data" looks like and what you think it should look when storing it?

      Check out QStyleItemDelegate

      • https://doc.qt.io/qt-6/qstyleditemdelegate.html

      for displaying custom model items.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      J 1 Reply Last reply
      2
      • H Offline
        H Offline
        Helmut.Jakoby
        wrote on last edited by
        #3

        I don't know much about Qt's model concept. If I understand you correctly, you want to have a data object for each cell in the table. In this case, I always created a subclass of the class (here, for example, QTableWidgetItem) that has a reference to a data object. I then inserted an object from this subclass into the QTableWidget. If I then wanted to work with a QTableWidgetItem from the table, I only had to cast to the subclass to access the data.

        https://www.globalobjects.de/

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @jdent said in How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?:

          I need to do the same in a QTableWidget

          That might be difficult. I think you have to use QTableView and implement the model + a proper delegate yourself.

          If I understand correctly, you want to store objects like CListBox (don't know what this exactly is) including the data they are currently displaying in a table?!
          Is CListBox more like QComboBox or more like QLineEdit?

          The whole approach seems weird. Wouldn't it be better to store the display data right away in the table model and then link the widgets to it somehow?!
          Why you want to store the widgets as "data" in your model?

          Can you give a example how your "data" looks like and what you think it should look when storing it?

          Check out QStyleItemDelegate

          • https://doc.qt.io/qt-6/qstyleditemdelegate.html

          for displaying custom model items.

          J Offline
          J Offline
          jdent
          wrote on last edited by
          #4

          @Pl45m4 My data is a vector of tuples all of identical structure. How hard is it to make a model out of this?
          I have a JoinedGridDisplayer that currently accepts the vector and displays it in the QTableWidget - or could be QTableView.

          So the question is should I create a model class to encapsulate this vector or should I derive from QTableWidgetItem so it references the rows/cols of the vector?

          JonBJ 1 Reply Last reply
          0
          • J jdent

            @Pl45m4 My data is a vector of tuples all of identical structure. How hard is it to make a model out of this?
            I have a JoinedGridDisplayer that currently accepts the vector and displays it in the QTableWidget - or could be QTableView.

            So the question is should I create a model class to encapsulate this vector or should I derive from QTableWidgetItem so it references the rows/cols of the vector?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @jdent
            QTableWidget is a "convenience" QTableView with its own model.

            The QTableWidget class provides an item-based table view with a default model.

            You seem to have your own model?

            If you want a table that uses your own data model you should use QTableView rather than this class.

            If you want to use your data structure directly derive from QAbstractItemModel (or QAbstractTableModel.). In principle you only need to do https://doc.qt.io/qt-6/qabstractitemmodel.html#subclassing:

            When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). These functions are used in all read-only models, and form the basis of editable models.

            Just 5 simple methods for a read-only model you can then show in a QTreeView. A few more if you want it editable. It's concise and easy to implement.

            1 Reply Last reply
            1
            • jeremy_kJ Online
              jeremy_kJ Online
              jeremy_k
              wrote on last edited by
              #6

              Without going into whether this is appropriate use or not, an easy solution is to store the data using a role that the delegate won't render.

              Eg:

              QAbstractItemModel *model = tableWidget.model();
              QModelIndex index = tableWidget.currentIndex().;
              QVariant data = getData();
              model->setData(index, data, ItemDataRole::UserRole)
              

              Asking a question about code? http://eel.is/iso-c++/testcase/

              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