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. How to implement visual effect for cutted item in QAbstractItemView without modifying the original data structures?
Forum Updated to NodeBB v4.3 + New Features

How to implement visual effect for cutted item in QAbstractItemView without modifying the original data structures?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 724 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.
  • jronaldJ Online
    jronaldJ Online
    jronald
    wrote on last edited by jronald
    #1

    As in file explorer, if you cut a file, the visual effect is that the file becoms grey or so.
    For QAbstractItemView, to implement the visual effect for cutting a item, do I have to add a flag to the item in the model?

    JonBJ 1 Reply Last reply
    0
    • jronaldJ jronald

      @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

      I don't know what you mean.

      E.g. QModelIndex model_index has row and column, when a item before it is removed, model_index.row() should be decreased by 1, but it doesn't happen automatically.

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

      @jronald
      So if you want whatever you need to know to be stored in the original item when it is cut, do so via setData(), then you don't need to remember/adjust the QModelIndex. You might store the desired "cut" icon either directly in setData(Qt::DecorationRole) or in your own Qt::UserRole + ... and test for that in data(Qt::DecorationRole)'s return value. Obviously whatever setData(role) value you use will need to be cleared out when the cut completes or is cancelled.

      jronaldJ 1 Reply Last reply
      2
      • jronaldJ jronald

        As in file explorer, if you cut a file, the visual effect is that the file becoms grey or so.
        For QAbstractItemView, to implement the visual effect for cutting a item, do I have to add a flag to the item in the model?

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

        @jronald
        I am thinking that would be one way of doing it, yes. By making data(Qt::DecorationRole) return a differently-colored icon while the "cut" is active. The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate(). See also the icon pictures at https://doc.qt.io/qt-5/qicon.html#making-classes-that-use-qicon.

        jronaldJ 1 Reply Last reply
        0
        • JonBJ JonB

          @jronald
          I am thinking that would be one way of doing it, yes. By making data(Qt::DecorationRole) return a differently-colored icon while the "cut" is active. The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate(). See also the icon pictures at https://doc.qt.io/qt-5/qicon.html#making-classes-that-use-qicon.

          jronaldJ Online
          jronaldJ Online
          jronald
          wrote on last edited by jronald
          #3

          @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

          The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate().

          So a flag in model item is always needed.

          The problem is e.g.

          an item is like this

          class Person
          {
          public:
              int id;
              std::string name;
              int age;
              std::shared_ptr<Person> parent
              std::shared_ptr<Persons> children;
          }
          

          By adding a flag for cutting status it'll like this:

          • Simply adding a flag mixes data for model with the essential data, not good enough
            class Person
            {
            public:
                int id;
                std::string name;
                int age;
                std::shared_ptr<Person> parent
                std::shared_ptr<Persons> children;
                bool is_cutted;
            }
            
          • With dedicated PersonItem for model, it is highly coupled with Person.<br>
            What's more worse is that PersonItem should also maintain the relationship about parent & children. Only the 3rd case below (i.e. derive PersonItem from Person), is ok, but to get parent/children of PersonItem, it gets them via interface of Person and cast them to PersonItem, it works but not perfect.
            class PersonItem
            {
            public:
                int id;
                std::string name;
                int age;
                std::shared_ptr<Person> parent
                std::shared_ptr<Persons> children;
                bool is_cutted;
            }
            
            or
            class PersonItem
            {
            public:
                Person Person;
                bool is_cutted;
            }
            
            or
            class PersonItem : public Person
            {
            public:
                bool is_cutted?
            }
            

          Maybe an extra class for saving status's like cut/copy is need, e.g.

          std::map<int, Status> item_status;
          

          The key is for student id, the value is for status.
          But it is obviously less efficient.

          JonBJ 1 Reply Last reply
          0
          • jronaldJ jronald

            @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

            The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate().

            So a flag in model item is always needed.

            The problem is e.g.

            an item is like this

            class Person
            {
            public:
                int id;
                std::string name;
                int age;
                std::shared_ptr<Person> parent
                std::shared_ptr<Persons> children;
            }
            

            By adding a flag for cutting status it'll like this:

            • Simply adding a flag mixes data for model with the essential data, not good enough
              class Person
              {
              public:
                  int id;
                  std::string name;
                  int age;
                  std::shared_ptr<Person> parent
                  std::shared_ptr<Persons> children;
                  bool is_cutted;
              }
              
            • With dedicated PersonItem for model, it is highly coupled with Person.<br>
              What's more worse is that PersonItem should also maintain the relationship about parent & children. Only the 3rd case below (i.e. derive PersonItem from Person), is ok, but to get parent/children of PersonItem, it gets them via interface of Person and cast them to PersonItem, it works but not perfect.
              class PersonItem
              {
              public:
                  int id;
                  std::string name;
                  int age;
                  std::shared_ptr<Person> parent
                  std::shared_ptr<Persons> children;
                  bool is_cutted;
              }
              
              or
              class PersonItem
              {
              public:
                  Person Person;
                  bool is_cutted;
              }
              
              or
              class PersonItem : public Person
              {
              public:
                  bool is_cutted?
              }
              

            Maybe an extra class for saving status's like cut/copy is need, e.g.

            std::map<int, Status> item_status;
            

            The key is for student id, the value is for status.
            But it is obviously less efficient.

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

            @jronald
            That is an implementation detail. A flag in the model would be an obvious choice but there are other ways of achieving this if you prefer, so long as your view can access the appropriate information from the QModelIndex passed to it.

            jronaldJ 1 Reply Last reply
            0
            • JonBJ JonB

              @jronald
              That is an implementation detail. A flag in the model would be an obvious choice but there are other ways of achieving this if you prefer, so long as your view can access the appropriate information from the QModelIndex passed to it.

              jronaldJ Online
              jronaldJ Online
              jronald
              wrote on last edited by
              #5

              @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

              so long as your view can access the appropriate information from the QModelIndex passed to it.

              Yes, but QModelIndex is not enough, e.g. after cutting an item and before pasting it, a sibling before it is deleted, QModelIndex would not be enough in this case.

              JonBJ 1 Reply Last reply
              0
              • jronaldJ jronald

                @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

                so long as your view can access the appropriate information from the QModelIndex passed to it.

                Yes, but QModelIndex is not enough, e.g. after cutting an item and before pasting it, a sibling before it is deleted, QModelIndex would not be enough in this case.

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

                @jronald
                I don't know what you mean. Each QModelIndex identifies one item in your model, and that is what is passed to a delegate to render it. That's all you have, and you decide in either data() or an item delegate how you want that item drawn.

                jronaldJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @jronald
                  I don't know what you mean. Each QModelIndex identifies one item in your model, and that is what is passed to a delegate to render it. That's all you have, and you decide in either data() or an item delegate how you want that item drawn.

                  jronaldJ Online
                  jronaldJ Online
                  jronald
                  wrote on last edited by
                  #7

                  @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

                  I don't know what you mean.

                  E.g. QModelIndex model_index has row and column, when a item before it is removed, model_index.row() should be decreased by 1, but it doesn't happen automatically.

                  JonBJ 1 Reply Last reply
                  0
                  • jronaldJ jronald

                    @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

                    I don't know what you mean.

                    E.g. QModelIndex model_index has row and column, when a item before it is removed, model_index.row() should be decreased by 1, but it doesn't happen automatically.

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

                    @jronald
                    So if you want whatever you need to know to be stored in the original item when it is cut, do so via setData(), then you don't need to remember/adjust the QModelIndex. You might store the desired "cut" icon either directly in setData(Qt::DecorationRole) or in your own Qt::UserRole + ... and test for that in data(Qt::DecorationRole)'s return value. Obviously whatever setData(role) value you use will need to be cleared out when the cut completes or is cancelled.

                    jronaldJ 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @jronald
                      So if you want whatever you need to know to be stored in the original item when it is cut, do so via setData(), then you don't need to remember/adjust the QModelIndex. You might store the desired "cut" icon either directly in setData(Qt::DecorationRole) or in your own Qt::UserRole + ... and test for that in data(Qt::DecorationRole)'s return value. Obviously whatever setData(role) value you use will need to be cleared out when the cut completes or is cancelled.

                      jronaldJ Online
                      jronaldJ Online
                      jronald
                      wrote on last edited by jronald
                      #9

                      @JonB said in How to implement visual effect for cutted item in QAbstractItemView?:

                      So if you want whatever you need to know to be stored in the original item when it is cut, do so via setData(), then you don't need to remember/adjust the QModelIndex. You might store the desired "cut" icon either directly in setData(Qt::DecorationRole) or in your own Qt::UserRole + ... and test for that in data(Qt::DecorationRole)'s return value. Obviously whatever setData(role) value you use will need to be cleared out when the cut completes or is cancelled.

                      I think it is the most efficient way. I'll use it.
                      Meanwhile the pure original class is also preferred, it can be a parameter to other functions.
                      It's likely that some convertions have to be there.

                      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