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



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


  • Lifetime Qt Champion

    Hi,

    Can you also post your MyModel declaration ?



  • 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).


  • Lifetime Qt Champion

    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 ?



  • 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



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



  • 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).


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.