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. QAbstractItemModel

QAbstractItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 702 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.
  • J Offline
    J Offline
    Jeff Barnes
    wrote on last edited by
    #1

    Hello all,
    I'm trying to remember how to use a QAbstractItemModel with a QListView.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->lvGroups->setModel(&m_groupModel);
    }
    ...
    void MainWindow::on_actionNew_Group_triggered()
    {
        GroupData grp(-1, "New Group");
        m_groupModel.addGroup(grp);
    }
    ...
    void GroupModel::addGroup(const GroupData &data)
    {
        qDebug() << "GroupModel::addGroup";
        beginInsertRows(QModelIndex(), m_groups.size(), m_groups.size());
        qDebug() << "appending group data";
        m_groups.append(data);
        qDebug() << "appended";
        endInsertRows();
    }
    ...
    QVariant GroupModel::data(const QModelIndex &index, int role) const
    {
        qDebug() << "GroupModel::data" << index.row();
        Q_UNUSED(role);
        if (!index.isValid())
            return QVariant();
        return m_groups.at(index.row()).name();
    }
    

    I don't see the group name in the list view, nor the appropriate qDebug() message in data(). I do see qDebug messages from addGroup().

    I must be missing something stupid. Could somebody help me out, please?

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

      Hi,

      That's because you are returning the same string for every role. You should be more selective. Return that string only for the DisplayRole and EditRole.

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

      J 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        That's because you are returning the same string for every role. You should be more selective. Return that string only for the DisplayRole and EditRole.

        J Offline
        J Offline
        Jeff Barnes
        wrote on last edited by
        #3

        @SGaist
        Thanks.
        But data() never gets called.

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

          Wrong model sizes ?

          Based on the code you shared, it's only guesses.

          Are you sure you correctly implemented all the required method ?

          You can check your model with the Qt model tester.

          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
          2
          • J Offline
            J Offline
            Jeff Barnes
            wrote on last edited by
            #5

            Bingo. Wrong model sizes. The QItemModel wizard puts logic that isn't appropriate for a QListView. If one isn't careful they will not implement rowCount() and columnCount() correctly. Pretty subtle mistake, actually.
            First of all, it will generate a compiler warning. Second, while the !parent.isValid() should be used to return the model size, return 0 makes me think it should always return 0. For QListViews, it should return the model size.
            The wizard could be more helpful, IMO.

            Mo' coffee.

            int RestaurantModel::rowCount(const QModelIndex &parent) const
            {
                if (!parent.isValid())
                    return 0;
            
                // FIXME: Implement me!
            }
            
            int RestaurantModel::columnCount(const QModelIndex &parent) const
            {
                if (!parent.isValid())
                    return 0;
            
                // FIXME: Implement me!
            }
            
            
            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