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. QTableView and Image
Forum Updated to NodeBB v4.3 + New Features

QTableView and Image

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 4.5k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #6
    • Where does m_image come from?
    • Are you subclassing QItemDelegate or QStyledItemDelegate?
    • Are you implementing myDelegate::sizeHint?

    LEAKFEST!

    The view does not own either the model or the delegate. You need to pass a parent to them or they are leaked

    "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

    M 1 Reply Last reply
    1
    • VRoninV VRonin
      • Where does m_image come from?
      • Are you subclassing QItemDelegate or QStyledItemDelegate?
      • Are you implementing myDelegate::sizeHint?

      LEAKFEST!

      The view does not own either the model or the delegate. You need to pass a parent to them or they are leaked

      M Offline
      M Offline
      MokJ
      wrote on last edited by MokJ
      #7

      thank you @VRonin , It was a mistake of mine ,that I didn't provide the complete information.
      I'm subclassing QItemDelegate....on this note how its different from subclassing QStyledItemDelegate?? I mean Qt document says that

      QStyledItemDelegate uses the current style to paint its items

      whats the current style here !!

      QSize myDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{
                return  QSize newSize(70,30);
      }
      
      QImage m_image(":/new/prefix1/d.jpg");
      

      m_image is in private of myDelegate.

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

        So m_image does not depend at all on what's in the model?

        @MokJ said in QTableView and Image:

        whats the current style here !!

        http://doc.qt.io/qt-5/style-reference.html

        Let's go step by step. The below should be an improvement already, correct?

        void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        (option.widget ? option.widget.style() : QApplication::style())->drawItemPixmap(painter,option.rect(),QPixmap::fromImage(m_image.scaled(option.rect().size())));
        }
        

        "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

        M 1 Reply Last reply
        2
        • VRoninV VRonin

          So m_image does not depend at all on what's in the model?

          @MokJ said in QTableView and Image:

          whats the current style here !!

          http://doc.qt.io/qt-5/style-reference.html

          Let's go step by step. The below should be an improvement already, correct?

          void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
          (option.widget ? option.widget.style() : QApplication::style())->drawItemPixmap(painter,option.rect(),QPixmap::fromImage(m_image.scaled(option.rect().size())));
          }
          
          M Offline
          M Offline
          MokJ
          wrote on last edited by
          #9

          @VRonin Thanks
          yes, m_image doesn't depend on model at all.
          I'm clear about current style now.

          Let's go step by step. The below should be an improvement already, correct?

          yes , after a small change It has improved.
          So whats the next step !
          What method should be used to get desired output !

          @MokJ said in QTableView and Image:

          Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?

          VRoninV 1 Reply Last reply
          0
          • M MokJ

            @VRonin Thanks
            yes, m_image doesn't depend on model at all.
            I'm clear about current style now.

            Let's go step by step. The below should be an improvement already, correct?

            yes , after a small change It has improved.
            So whats the next step !
            What method should be used to get desired output !

            @MokJ said in QTableView and Image:

            Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?

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

            @MokJ said in QTableView and Image:

            So whats the next step !

            We make the image depend on the model.
            In the constructor add:

            connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,[view](const QItemSelection &selected, const QItemSelection &deselected)->void{
            QModelIndexList indexList = selected.indexes();
            for(auto& index : qAsConst(indexList))
            view->model()->setData(index,true,Qt::UserRole);
            indexList = deselected.indexes();
            for(auto& index : qAsConst(indexList))
            view->model()->setData(index,QVariant(),Qt::UserRole);
            });
            

            now in myDelegate::paint you can add if(index.data(Qt::UserRole).toBool()) to check if an item is selected or not

            "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

            M 1 Reply Last reply
            3
            • VRoninV VRonin

              @MokJ said in QTableView and Image:

              So whats the next step !

              We make the image depend on the model.
              In the constructor add:

              connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,[view](const QItemSelection &selected, const QItemSelection &deselected)->void{
              QModelIndexList indexList = selected.indexes();
              for(auto& index : qAsConst(indexList))
              view->model()->setData(index,true,Qt::UserRole);
              indexList = deselected.indexes();
              for(auto& index : qAsConst(indexList))
              view->model()->setData(index,QVariant(),Qt::UserRole);
              });
              

              now in myDelegate::paint you can add if(index.data(Qt::UserRole).toBool()) to check if an item is selected or not

              M Offline
              M Offline
              MokJ
              wrote on last edited by
              #11

              @VRonin Thank you
              I have added that snippet too, now question is , How is it making m_image depend on model ?

              VRoninV 1 Reply Last reply
              0
              • M MokJ

                @VRonin Thank you
                I have added that snippet too, now question is , How is it making m_image depend on model ?

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

                @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                P.S.
                if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                "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

                M H 3 Replies Last reply
                3
                • VRoninV VRonin

                  @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                  P.S.
                  if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                  M Offline
                  M Offline
                  MokJ
                  wrote on last edited by MokJ
                  #13

                  @VRonin
                  I'm a bit confused about how its supposed to work. I mean once I select an Index it goes inside

                  if(index.data(Qt::UserRole).isValid()) {}
                  

                  What should get implemented in If?

                  I appreciate your help greatly.

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                    P.S.
                    if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                    H Offline
                    H Offline
                    hjohn
                    wrote on last edited by hjohn
                    #14

                    @VRonin
                    can we take the Image as string ?

                    M 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                      P.S.
                      if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                      M Offline
                      M Offline
                      MokJ
                      wrote on last edited by
                      #15

                      @VRonin
                      I got it,
                      in If , I'm drawing the new Image.
                      Thanks very much @VRonin.
                      Now If we want to keep that image in model should we implement

                      void setModelData(QWidget*editor,QAbstractItemModel *model,const QModelIndex &index)const
                      

                      again thanks @VRonin .

                      1 Reply Last reply
                      0
                      • H hjohn

                        @VRonin
                        can we take the Image as string ?

                        M Offline
                        M Offline
                        MokJ
                        wrote on last edited by
                        #16

                        @hjohn
                        I came across this , maybe it could be helpful.

                        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