Can't setHeaderData in class derived from QAbstractTableModel[SOLVED]



  • Hello again.

    I tried to create my own model derived from QAbstractTableModel. I have inherited it like that:

    class MyTableModel: public QAbstractTableModel {
    ....
    public:
        int rowCount(const QModelIndex& index) const;
        int columnCount(const QModelIndex& index) const { return 2; }
        QVariant data(const QModelIndex & index, int role) const;
    ....
    }
    

    My model is working fine except I can't change the header of row:

    MyTableModel model;
    bool is_change = model.setHeaderData(0, Qt::Horizontal, "Column1");
    qDebug() << is_change;
    

    All time in console I see "false".
    Also I tried reimplement this method with calling headerDataChanged(), but the false answer still exists.

    Please, help me to change header data)
    Thank you


  • Lifetime Qt Champion

    Hi,

    Maybe a silly question but: do you have at least one column in your model ?



  • @SGaist
    No. So I can set Header Name only after I have create first row?


  • Lifetime Qt Champion

    AFAIK, you can't set a header on a row/column that doesn't exist. The row/column can be empty, but it still needs to exist



  • @SGaist

    Sorry.
    I understood my mistake.
    The point is that I have done mistake in my previous code.
    I have two columns(not two rows) and trying to specify one of them.



  • It's again me.
    The only thing which I needed to do - to reimplement this method :

    QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const.
    

    Thanks


Log in to reply
 

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