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. QAbstractTableModel hard to set up
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel hard to set up

Scheduled Pinned Locked Moved Unsolved General and Desktop
model-viewqtablewidgetiteqtablewidgetqabstracttablemqtableview
3 Posts 3 Posters 1.6k 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.
  • moffa13M Offline
    moffa13M Offline
    moffa13
    wrote on last edited by
    #1

    Hi,

    I was trying to implement a QAbstractTableModel with QTableView but I find the model operating a bit hard to use, i'll explain.

    I have a list of QString associated with structures containing lot of informations; it's reprensented by a QMap<String, Infos>. Some data in Infos have to be shown in the QTableView.

    If I understand correctly, the model holds the container and we have to emit a datachanged signal each time we modify something that's rendered into the QTableView. So I have to make kinds of setters, I think.

    The problem is that before, I had a QTableWidget and I used QTableWidgetItem* into the Infos struct so when i modified the content of a QTableWidgetItem, it was directly rendered in the table.

    Now, let's say it's a QString instead of QTableWidgetItem, I have to use my setters like
    m_model.setSize("file.txt", "25MB");

    In my setter I have to change the size associated with the key (file.txt) like this

    void FilesListModel::setSize(const QString &name, QString const& size){
        m_infos[name].size = size; // m_infos is QMap<QString, Infos>
        emit dataChanged(index(row, 1), index(row, 1));
    }
    

    I find it really annoying to repeat making setters for each column.
    Also, How to find the corresponding row of a key (file.txt) ?

    Please tell me if I misunderstood the way we use the models.

    Thanks for your help!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Then why not have a setter for your custom structure ?

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

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        In 90% of the cases you can use QStandardItemModel instead of going through the minefield that is subclassing an abstract model.

        My advice is just to use that "universal model" instead of a custom one. If you really, really want to customise it as performance of QStandardItemModel is a problem then make sure you run your model through the Model Test (needs just a couple of trivial fixes to work on Qt5) that will tell you if you did everything as you were supposed to or you fell in the countless pitfalls of model subclassing

        "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
        0

        • Login

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