Important: Please read the Qt Code of Conduct -

[SOLVED] Questions about implementing custom Model

  • I've been reading up on implementing a custom model, and I want to make sure I'm understanding things correctly before I delve in.

    From what I understand I need to implement rowCount() and data(), now it seems that data() is called on each "row" of data in the model, and the QVariant being returned is what is actually being displayed, correct? Now if I don't want a specific row to be displayed I would just return a blank QVariant(), is that correct?

    My next question is this, it seems that in the model you only insert blank rows, and it's all up to your implementation the data() method to return the object actually being displayed, is this correct? Or are you supposed to use setData() after each row you add? Or is setData() only meant for special circumstances where the data is being changed after data() was already called on the row (like when it's editable)?

  • Lifetime Qt Champion


    It depends on what type of model you are talking about: list, table, tree ? Depending on that you'll have implement more than just rowCount.

    Indeed, data is called but it returns much more than the text displayed. Read about the role parameter to see what your model can return in order to customize the view's output.

    setData is used when you want to modify something in your model, be it from an editor or another source. It's the central point where you modify your model like data is the central point to retrieve its content.

    No, you don't insert blank rows. This is valid if you are using one of the convenient classes like QTableWidget, QListWidget and friends. There you have to "configure" the underlying model before inserting data in it. Otherwise the view will ask your model for a row count so then it will try to retrieve as much data.

    Hope it helps

Log in to reply