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. [SOLVED] Questions about implementing custom Model
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Questions about implementing custom Model

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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)?

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

      Hi,

      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

      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