[Worked around] How to set header text of QAbstractItemModel in a QTreeView

  • I have a QTreeView, and its model is (a subclass of) a QAbstractItemModel.

    The model has two columns, and they are labelled "1" and "2". I suspect this is the default labels.

    I want them to be blank, so I put this into the constructor of the model to test my ability to set the header text:

    @bool worked = setHeaderData(1, Qt::Horizontal, QVariant(QString("Beans")));@

    Worked has the value "false". It does not work. The label does not get changed. I tried this as well:

        @setHeaderData(1, Qt::Horizontal, QVariant(QString("Beans")), Qt::DisplayRole);@

    and this also does not work.

    How can I change the header text of a QAbstractItemModel?

  • I have read them. I would say they are an acceptable introduction; I have read technical documents that really are "very good", and these are not. They're acceptable, but not "very good". :) I can't recall a "very good" set of online documentation right now, but Maple's online documentation is an example of some truly horrific documentation. :)

    Thanks very much. In the end, I overrode the QAbstractItemModel::headerData() function and simply intercepted any calls that wanted DisplayRole data. A bit of a silly solution, but it works.

  • Huh?? What do you mean with:
    bq. intercepted any calls that wanted DisplayRole data
    The hole point of Model/View usage is that the model holds the data, not the view or anyone else........

  • I mean that my subclass of QAbstractItemModel now has a function headerData(), so when the headerdata is required and that function is called, it goes through my code and I simply return the value I want.

    I have no idea what value is held in the model for the header text; because I don't know how to change the value for the header text in the model, I don't fetch it from the model. Calling the model's setHeaderData function to try to set the header data did not work.

  • No, you have to implement/overwrite the setHeaderData your self and store the information in your model.

Log in to reply

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