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 implementation: QVariant MyModel::data(const QModelIndex &index, int role) const is never called.
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel implementation: QVariant MyModel::data(const QModelIndex &index, int role) const is never called.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 4.2k 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.
  • R Offline
    R Offline
    Rhadel
    wrote on last edited by
    #1

    Hi there,

    I ve my custom QAbstractTableModel implementation with its data method implemented:

    @
    QVariant MyModel::data(const QModelIndex &index, int role) const{
    qDebug() << "Never called";

    .....
    }
    @

    This method is never called, but the others are: columnCount, rowCount, headerData. Counters return a correct value for my QVector<CustomClass *> containter, that has the information displayable in the table.
    My vector is stored in another class, as a member of an static members obect (Singleton) in order to be accesible from anywhere in the application. Row couting comes from its .count method and its Ok. Columns are just 2. Header are shown properly. Data should return the proper value from this QVector, but It's never called instead.

    Whats wrong? :S

    Thank you.

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

      Hi,

      Can you also post your MyModel declaration ?

      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
      • R Offline
        R Offline
        Rhadel
        wrote on last edited by
        #3

        Sure:

        @
        class MyModel : public QAbstractTableModel
        {
        Q_OBJECT
        friend class TableChromoDelegate;
        public:
        explicit MyModel(QObject *parent = 0);

        //QModelIndex index(int row, int column, const QModelIndex &parent) const;
        //QModelIndex parent(const QModelIndex &child) const;
        int rowCount(const QModelIndex &parent) const;
        int columnCount(const QModelIndex &parent) const;
        QVariant data(const QModelIndex &index, int role) const;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;
        //bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role);
        
        void redraw();
        

        protected:

        signals:

        public slots:

        };
        @

        Data is storen in an static variable as a QVector, and accessed when needed from anywhere (also in MyModel implementation).

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

          Seems correct... Just wondering why you have a delegate as a friend of your model.

          On what type of view are you setting your model ?

          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
          • R Offline
            R Offline
            Rhadel
            wrote on last edited by
            #5

            Well, I can remove the delegate friend class, its ok. It was here for an older implementation.

            I'm using this model on a QTableView inside a QWidget (PanelChromo) created with QtCreator (I set the model in its constructor):

            @

            PanelChromo::PanelChromo(QWidget parent) :
            QWidget(parent),
            ui(new Ui::PanelChromo)
            {
            delegate = new TableChromoDelegate(this);
            TableChromoModel
            model = new TableChromoModel(this);
            ui->setupUi(this);
            ui->tableView->setItemDelegate(delegate);
            ui->tableView->setModel(model); //Setting model.
            //this->show();
            }

            @

            QtCreator has declared QTableView as follow:

            @
            class Ui_PanelChromo
            {
            public:
            QHBoxLayout *horizontalLayout;
            QTableView *tableView;

            void setupUi(QWidget *PanelChromo) {
            

            ....

            @

            Thank you for your replies

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rhadel
              wrote on last edited by
              #6

              Well, It means in use it on a QTableView object.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rhadel
                wrote on last edited by
                #7

                Ok, I solved it, just by rewriting the full class. I dunno what was wrong in the old code.

                Anyway, maybe there is a better way to handle the information stored in my static QVector, because when I call:

                @
                bool MyModel::insertRows(int row, int count, const QModelIndex &parent)
                {
                qDebug () << "Insert rows called";
                QAbstractItemModel::beginInsertRows(parent, 0, ControlUnit::control->staticVar.count());
                QAbstractItemModel::endInsertRows();
                return false;
                }

                @

                As you see, I dont really need the parameters, because is all at staticVar static variable. It seems to be not clean.

                headerData is not called properly (never with Qt::DisplayRole value in role parameter).

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

                  The function also doesn't do anything so it's not really useful like that.

                  Without the rest of the MyModel I can't really help you diagnose anything

                  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
                  • R Offline
                    R Offline
                    Rhadel
                    wrote on last edited by
                    #9

                    insertRows has not implentation from parent class QAbstractItemModel. I just need a signal to warn MyModel the data has changed. As It seems that emit dataChanged has not effect for some reason, reimplement "insertRows" and call this method when my data is ready to being shown works for me (once called, Qt calls finally data function, so only by calling this insertRow function Qt demands data). Data function iterates ControlUnit::control->staticVar.count() and returns the appropiate data.

                    Surely I misunderstood when data function is called and why Its called after insertRows .

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

                      Are you sure you are emitting dataChanged with the right parameters ?

                      the begin/end functions are there to tell the model that there is something that will happen that changes the model.

                      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

                      • Login

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