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. Data method implementation in QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

Data method implementation in QStandardItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.5k 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.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by A Former User
    #1

    Hi All,

    Is there any hint to (re-)implement data method in QStandartItemModel used with QTreeView?
    For some reasons, I need to control my QStandardItem array in the model such as highlighting/updating a specific row.
    I have no luck so far with setIndexWidget and setData.
    How can I return or set a QStandartItem in DisplayRole within the model?

    Any ideas?

    Many thanks in advance for your help.
    Regards,
    Sat

    VRoninV 1 Reply Last reply
    0
    • S ShinSat

      Hi All,

      Is there any hint to (re-)implement data method in QStandartItemModel used with QTreeView?
      For some reasons, I need to control my QStandardItem array in the model such as highlighting/updating a specific row.
      I have no luck so far with setIndexWidget and setData.
      How can I return or set a QStandartItem in DisplayRole within the model?

      Any ideas?

      Many thanks in advance for your help.
      Regards,
      Sat

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

      @ShinSat said in data method implementation in QStandardItemModel:

      highlighting/updating a specific row

      void highlightRow(QAbstractItemModel* const mdl, int row, const QModelIndex& parent=QModelIndex()){
      Q_ASSERT(!parent.isValid() || parent.model()==mdl);
      Q_ASSERT(row>=0 && row<mdl->rowCount(parent));
      const int colCount =mdl->columnCount(parent)
      for(int i=0;i<colCount ;++i)
      mdl->setData(mdl->index(row,i,parent),QBrush(Qt::red),Qt::BackgroundRole);
      }
      void unHighlightRow(QAbstractItemModel* const mdl, int row, const QModelIndex& parent=QModelIndex()){
      Q_ASSERT(!parent.isValid() || parent.model()==mdl);
      Q_ASSERT(row>=0 && row<mdl->rowCount(parent));
      const int colCount =mdl->columnCount(parent)
      for(int i=0;i<colCount ;++i)
      mdl->setData(mdl->index(row,i,parent),QVariant(),Qt::BackgroundRole);
      }
      
      

      "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

      S 1 Reply Last reply
      1
      • VRoninV VRonin

        @ShinSat said in data method implementation in QStandardItemModel:

        highlighting/updating a specific row

        void highlightRow(QAbstractItemModel* const mdl, int row, const QModelIndex& parent=QModelIndex()){
        Q_ASSERT(!parent.isValid() || parent.model()==mdl);
        Q_ASSERT(row>=0 && row<mdl->rowCount(parent));
        const int colCount =mdl->columnCount(parent)
        for(int i=0;i<colCount ;++i)
        mdl->setData(mdl->index(row,i,parent),QBrush(Qt::red),Qt::BackgroundRole);
        }
        void unHighlightRow(QAbstractItemModel* const mdl, int row, const QModelIndex& parent=QModelIndex()){
        Q_ASSERT(!parent.isValid() || parent.model()==mdl);
        Q_ASSERT(row>=0 && row<mdl->rowCount(parent));
        const int colCount =mdl->columnCount(parent)
        for(int i=0;i<colCount ;++i)
        mdl->setData(mdl->index(row,i,parent),QVariant(),Qt::BackgroundRole);
        }
        
        
        S Offline
        S Offline
        ShinSat
        wrote on last edited by
        #3

        @VRonin Thanks for an update!
        SO,,,, I can switch the two methods with QTimer or something, correct?
        Also, one thing I'm not sure is if I can return QStandartItem from Displayrole of QStandartItemModel becaue it's not working in my environment, which is PyQt5.1 on Win7.
        Is this a recommended or a proven way to handle custom QStandardItems between QStandardItemModel and QTreeView?

        Sat

        VRoninV JonBJ 2 Replies Last reply
        0
        • S ShinSat

          @VRonin Thanks for an update!
          SO,,,, I can switch the two methods with QTimer or something, correct?
          Also, one thing I'm not sure is if I can return QStandartItem from Displayrole of QStandartItemModel becaue it's not working in my environment, which is PyQt5.1 on Win7.
          Is this a recommended or a proven way to handle custom QStandardItems between QStandardItemModel and QTreeView?

          Sat

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

          @ShinSat said in data method implementation in QStandardItemModel:

          if I can return QStandartItem from Displayrole of QStandartItemModel

          You can but I don't see the point.
          QStandartItemModel::data() calls QStandartItem::data(). Also, if you return QStandartItem for that role you'll also have to implement a custom delegate that can handle displaying QStandartItem

          it's not working in my environment

          In C++ you just need to use QVariant::fromValue but since it's a template I'm not sure how it translates to Python

          "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
          • S ShinSat

            @VRonin Thanks for an update!
            SO,,,, I can switch the two methods with QTimer or something, correct?
            Also, one thing I'm not sure is if I can return QStandartItem from Displayrole of QStandartItemModel becaue it's not working in my environment, which is PyQt5.1 on Win7.
            Is this a recommended or a proven way to handle custom QStandardItems between QStandardItemModel and QTreeView?

            Sat

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @ShinSat

            @Vronin wrote:

            In C++ you just need to use QVariant::fromValue but since it's a template I'm not sure how it translates to Python

            If you think you're having a Python/PyQt issue, post your Python/PyQt for this here and I'll have a look....

            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