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. The role of QStandardItems
Qt 6.11 is out! See what's new in the release blog

The role of QStandardItems

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.2k 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.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by
    #1

    Hi,

    for a long time, I've stopped working on Qt projects and, again, I have to read lot about the basics.
    I'm writing a small project that handles some financials and I need a QTableView, for which I subclassed QStandardItemModel.

    My basic and maybe stupid question is: Do I mandatory need QStandardItems inside the views cells (or inside the model) to handle with the data or is it possible to store data "directly" into the cells? I'm asking that because while reading the documentation I see that it is recommended to generate TreeViews and ListViews empty and appendRows/Columns with directly insering items, while it is recommended for TableViews to scale directly with int row & int column by constructing the model and inserting items with setItem.
    Well, I created such a QTableView with an (empty?) QStandardItemModel with 10x10 matrix and as I can see, without setting items, it is possible to double click a cell and store data. So, I'm asking myself if:
    a) there are QStandardItems generated by default for each index (cell) or
    b) if it is generated by the delegate while typing into the cell or
    c) there exists a way of directly storing data into the view (or model?) without having created corresponding items

    Can you help me with that, I don't find the answer in the docs, unfortunately.

    Thank you very much for your help!
    Kind regards
    Binary91

    JonBJ 1 Reply Last reply
    0
    • B Binary91

      Hi,

      for a long time, I've stopped working on Qt projects and, again, I have to read lot about the basics.
      I'm writing a small project that handles some financials and I need a QTableView, for which I subclassed QStandardItemModel.

      My basic and maybe stupid question is: Do I mandatory need QStandardItems inside the views cells (or inside the model) to handle with the data or is it possible to store data "directly" into the cells? I'm asking that because while reading the documentation I see that it is recommended to generate TreeViews and ListViews empty and appendRows/Columns with directly insering items, while it is recommended for TableViews to scale directly with int row & int column by constructing the model and inserting items with setItem.
      Well, I created such a QTableView with an (empty?) QStandardItemModel with 10x10 matrix and as I can see, without setting items, it is possible to double click a cell and store data. So, I'm asking myself if:
      a) there are QStandardItems generated by default for each index (cell) or
      b) if it is generated by the delegate while typing into the cell or
      c) there exists a way of directly storing data into the view (or model?) without having created corresponding items

      Can you help me with that, I don't find the answer in the docs, unfortunately.

      Thank you very much for your help!
      Kind regards
      Binary91

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

      @Binary91 said in The role of QStandardItems:

      b) if it is generated by the delegate while typing into the cell or

      This one.

      QStandardItemModel is just a convenience pre-supplied model which is generic. It lets you easily create data items of any type, and it provides storage for any of the other roles' item data if desired (like color or size of checkability). It provides the data storage. You can also still use data()/setData() on the model if you don't want to go via "items".

      But if you have your own data storage/types already written then copying these to & from QStandardItemModel can be wasteful. There you might want to write your own to inherit from QAbstractItemModel and operate directly on your own existing data for efficiency/type safety.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        Hi,
        thank you for your reply!
        Ok, so as the corresponding item object is created while using a delegate to store data into the views cells, I guess that there can't be data inside the view without a corresponding item object that holds it, right? I was just wondering why, in that case of behaviour, it is possible to retrive an item with a QModelIndex to an empty cell that never stored any data and that I never created an item by myself for...

        "But if you have your own data storage/types already written then copying these to & from QStandardItemModel can be wasteful. There you might want to write your own to inherit from QAbstractItemModel and operate directly on your own existing data for efficiency/type safety."

        Yes, you're right, I think I'll have to do it this way in future projects.. Thank you!

        JonBJ 1 Reply Last reply
        0
        • B Binary91

          Hi,
          thank you for your reply!
          Ok, so as the corresponding item object is created while using a delegate to store data into the views cells, I guess that there can't be data inside the view without a corresponding item object that holds it, right? I was just wondering why, in that case of behaviour, it is possible to retrive an item with a QModelIndex to an empty cell that never stored any data and that I never created an item by myself for...

          "But if you have your own data storage/types already written then copying these to & from QStandardItemModel can be wasteful. There you might want to write your own to inherit from QAbstractItemModel and operate directly on your own existing data for efficiency/type safety."

          Yes, you're right, I think I'll have to do it this way in future projects.. Thank you!

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

          @Binary91
          So long as a row/column valid, data(index) will return an empty/invalid QVariant(). That's probably what "an empty item" is. The editor delegate will create a new item.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on last edited by
            #5

            Hi JonB,
            sorry for the belated response. Ok, that sonds logical to me. So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data. That was what I needed to realize.

            Well, in your first response, you recommended to maybe create my own model by directly inheriting from QAbstractItemModel in case I'd have me own data storage or types. I'd like to work with table data that I will store in a SQL database and load it into the model when I need it. So far, I don't have specific item types, I'd try to work on the QStandardItem. Long time ago, I started a try of subclassing QAbstractItemModel and I realized that it is very hard work, especially handling inserting of new columns and stuff. I thought of QStandardItemModel to be such a basic subclass of it that already fits the generic stuff I need for my basic usage. Well, I'll maybe see it while using it...

            Anyway, thank you for your help!

            JonBJ 1 Reply Last reply
            0
            • B Binary91

              Hi JonB,
              sorry for the belated response. Ok, that sonds logical to me. So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data. That was what I needed to realize.

              Well, in your first response, you recommended to maybe create my own model by directly inheriting from QAbstractItemModel in case I'd have me own data storage or types. I'd like to work with table data that I will store in a SQL database and load it into the model when I need it. So far, I don't have specific item types, I'd try to work on the QStandardItem. Long time ago, I started a try of subclassing QAbstractItemModel and I realized that it is very hard work, especially handling inserting of new columns and stuff. I thought of QStandardItemModel to be such a basic subclass of it that already fits the generic stuff I need for my basic usage. Well, I'll maybe see it while using it...

              Anyway, thank you for your help!

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

              @Binary91 said in The role of QStandardItems:

              So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data.

              There is nothing magical here. It's just that the QTableView/Widget editing delegate has QAbstractItemDelegate::setModelData() method. But you can always create/update QStandardItems yourself via QStandardItemModel methods. The view simply makes whatever appropriate calls to the model, nothing you cannot do explicitly.

              I'd like to work with table data that I will store in a SQL database and load it into the model when I need it.

              It would be simpler if you use Qt's QSql... classes, including models and queries, rather than extracting stuff out to copy into abstract or standard item models. QTableView still works just as much with SQL models as item models.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Binary91
                wrote on last edited by
                #7

                Again, sorry for replying so late.

                Thank you very much for that hint using QSql itsself as a model. I will read the Qt documentation about that!

                Kind Regards,
                Binary

                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