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. Set datatype for a column in a QStandardItemModel

Set datatype for a column in a QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k Views
  • 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.
  • T Offline
    T Offline
    TheFlyingMooseMan
    wrote on last edited by
    #1

    I use a QStandardItemModel so I can populate this model on my own.
    However, QStandardItemModel::setItem only accepts QStandardItems, that only be instantiated with text (QString).
    The first column is a number (integer) -- sorting on this column will use string sorting instead of numerical sorting, eg.:

    1
    10
    11
    2
    3
    

    ..
    Instead of:

    1
    2
    3
    ..
    10
    11
    ..
    

    How do I handle this/set this up?

    Thanks,
    Roy

    raven-worxR 1 Reply Last reply
    0
    • T TheFlyingMooseMan

      I use a QStandardItemModel so I can populate this model on my own.
      However, QStandardItemModel::setItem only accepts QStandardItems, that only be instantiated with text (QString).
      The first column is a number (integer) -- sorting on this column will use string sorting instead of numerical sorting, eg.:

      1
      10
      11
      2
      3
      

      ..
      Instead of:

      1
      2
      3
      ..
      10
      11
      ..
      

      How do I handle this/set this up?

      Thanks,
      Roy

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @TheFlyingMooseMan
      most straight forward way would be to use QSortFilterProxyModel on top of your current model and reimplement the lessThan() method.
      Check if the passed index has the column number and return your comparison. For all other columns call the base class implementation.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TheFlyingMooseMan
        wrote on last edited by
        #3

        I was hoping for a more elegant solution, but subclassing QSortFilterProxyModel and reimplemting the lessThan() method works. Thanks raven-worx!

        VRoninV 1 Reply Last reply
        0
        • T TheFlyingMooseMan

          I was hoping for a more elegant solution, but subclassing QSortFilterProxyModel and reimplemting the lessThan() method works. Thanks raven-worx!

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @TheFlyingMooseMan said in Set datatype for a column in a QStandardItemModel:

          that only be instantiated with text (QString)

          Look beyond the constructor. create the QStandardItem with the default constructor and then call setData on it passing the number

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved