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. regarding role in setData method of QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

regarding role in setData method of QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 2 Posters 4.3k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    I have a function in which I show a image in first column of each row . I return a QIcon in ::data . My requirement is to edit the image when I double click on this image

    class myView : public QTreeView{
    private:
    myModel d_m;
    };

    class myModel:public QAbstractItemmodel
    };

    myView::myview() {
    myModel m;
    setModel(m);
    d_m =m;
    }

    void myInstView::clickInstance(const QModelIndex& index)
    {
    d_m->setData(setData(currentIndex(),color,Qt::DecorationRole);;
    }

    boo myModel::setData(const QModelIndex& index,const QVariant& variant, const int role)
    {
    if (role == Qt::DecorationRole && index.column() == 1) {
    QColor color = variant.value<QColor>();
    myMap[pointer] = image;
    addIamgeToMap(image);
    /* set color for all the visible children
    }
    }

    QVariant myModel::data(const QModelIndex& index, int role) const
    {
    if(index.coumn ==1) && role== Decoration role {
    return myMap[pointer] ;
    }
    }

    My Query is is sending Decoration in setData is fine and is the logic is also fine ?

    VRoninV 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Doing this kind of things is exactly what delegates are made for. take a look at http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes and QStyledItemDelegate

      "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
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        I took help from this example

        http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

        what is the problem in my code

        1 Reply Last reply
        0
        • Q Qt Enthusiast

          I have a function in which I show a image in first column of each row . I return a QIcon in ::data . My requirement is to edit the image when I double click on this image

          class myView : public QTreeView{
          private:
          myModel d_m;
          };

          class myModel:public QAbstractItemmodel
          };

          myView::myview() {
          myModel m;
          setModel(m);
          d_m =m;
          }

          void myInstView::clickInstance(const QModelIndex& index)
          {
          d_m->setData(setData(currentIndex(),color,Qt::DecorationRole);;
          }

          boo myModel::setData(const QModelIndex& index,const QVariant& variant, const int role)
          {
          if (role == Qt::DecorationRole && index.column() == 1) {
          QColor color = variant.value<QColor>();
          myMap[pointer] = image;
          addIamgeToMap(image);
          /* set color for all the visible children
          }
          }

          QVariant myModel::data(const QModelIndex& index, int role) const
          {
          if(index.coumn ==1) && role== Decoration role {
          return myMap[pointer] ;
          }
          }

          My Query is is sending Decoration in setData is fine and is the logic is also fine ?

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

          @Qt-Enthusiast said:

          I have a function in which I show a image in first column of each row

          you are checking for index.coumn()==1 so that's the second column

          class myView : public QTreeView{
          private:
          myModel d_m;
          };

          Usually it's not a great idea to make the view own the model. you can do it (that's what the Q*Widgets do) but it's often not the best way

          class myModel:public QAbstractItemmodel
          };

          myView::myview() {
          myModel m;
          setModel(m);
          d_m =m;

          how can you do this? QObjects can't be copied also the logic here is wrong as you store a copy of the model but then use the original one as base for the view

          void myInstView::clickInstance(const QModelIndex& index)

          what is myInstView?

           return myMap[pointer] ;
          

          What is pointer?

          the logic is also fine ?

          using the decoration role to display the image is 100% correct but to edit the image you should use a delegate

          "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
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            I am using qt.4.3.3 and qstyleditemdelegate.html is available fro,m qt4.4 so I cannot used qstyleditemdelegat please let me know if I impilimentation is ok

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              you can use QItemDelegate, the only difference is that QStyledItemDelegate uses stylesheets while QItemDelegate doesn't http://doc.qt.io/qt-4.8/qitemdelegate.html

              "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
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                can u point me exact example where QtreeView , customodel and a deletegate is used so that I understand the things better

                Since storing a model pointer in View is my legacy code can I call

                class myView::QTreeView {
                }

                void myView::clicik() {
                d_m->setData
                }

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  create a class that inherits from QItemDelegate and reimplement

                  virtual QWidget* createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
                  virtual void setEditorData(QWidget * editor, const QModelIndex & index) const;
                  virtual void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
                  virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
                  virtual void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
                  

                  the widget returned from createEditor is displayed inside the view cell during editing, setEditorData is used to load data from the model to the editor, setModelData is used to save data from the editor to the model.

                  updateEditorGeometry is used to resize the editor in case the view gets resized during editing and sizeHint tells the view how much space your image should take

                  finally you use QTreeView::setItemDelegateForColumn to set the delegate on the view and voilà

                  you can use QTreeView::setEditTriggers to chose when your editor should pop up ( QAbstractItemView::DoubleClicked I think in your case)

                  "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
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    My question is what is advantage of using delegate since my legacy code my View owns the model pointer

                    is there any advandathe

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      not knowing what myInstView is I can't provide a definitive answer but handling your editor widget lifetime, positioning it correctly, resizing it correctly and handling when the editing starts and finishes all out of the box are pretty big advantages for me

                      "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
                      0
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on last edited by
                        #11

                        it is a object of QTreeVIew

                        1 Reply Last reply
                        -1
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          That's not particularly helpful... what class is it? a button? a lineedit?

                          "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
                          0
                          • Q Offline
                            Q Offline
                            Qt Enthusiast
                            wrote on last edited by
                            #13

                            a QTreeView

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              I don't get the difference between myInstView and myView but it does not matter. Delegate is the answer.

                              My requirement is to edit the image when I double click on this image

                              if you want any more details you need to describe better how the image should be changed. is it selected from a combobox, is it a color button, is it loaded from file?

                              "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
                              0

                              • Login

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