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. Changing an image using QStyledItemDelegate in TableView
Forum Updated to NodeBB v4.3 + New Features

Changing an image using QStyledItemDelegate in TableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 5.3k Views 3 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.
  • V Offline
    V Offline
    VRonin
    wrote on 16 Jan 2017, 08:28 last edited by
    #10

    I posted the complete code above that takes care of everything, just use that one.

    if((index.column () == 3))

    The delegate should never have this kind of code inside. The delegate should not depend on the model. use setItemDelegateForColumn in the view to obtain that result

    "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

    G 1 Reply Last reply 17 Jan 2017, 04:14
    2
    • V VRonin
      16 Jan 2017, 08:28

      I posted the complete code above that takes care of everything, just use that one.

      if((index.column () == 3))

      The delegate should never have this kind of code inside. The delegate should not depend on the model. use setItemDelegateForColumn in the view to obtain that result

      G Offline
      G Offline
      gabor53
      wrote on 17 Jan 2017, 04:14 last edited by gabor53
      #11

      @VRonin
      When I try to implement your code I end up with different error messages.
      If I leave everything in the .h file like this:

      virtual void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE
              {
                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                  Q_ASSERT(imgPick);
                  if(imgPick->selectedFile().isEmpty())
                      {
                          model->setData(index,QVariant(),Qt::UserRole);
                          model->setData(index, QVariant(),Qt::DecorationRole);
                      }
                  else
                      {
                          model->setData(index,imgPick->selectedFile(),Qt::UserRole);
                          model->setData(index, QIcon(imgPick->selectedFile()),Qt::DecorationRole);
                      }
              }
      
              virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE
              {
                  ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
                  Q_ASSERT(imgPick);
                  if(imgPick->selectedFile().isEmpty())
                      {
                          model->setData(index,QVariant(),Qt::UserRole);
                          model->setData(index, QVariant(),Qt::DecorationRole);
                      }
                  else
                      {
                          model->setData(index,imgPick->selectedFile(),Qt::UserRole);
                          model->setData(index, QIcon(imgPick->selectedFile()),Qt::DecorationRole);
                      }
              }
      
      

      I get an error message saying expected token ';' got 'const'. If I move the definition to the cpp file I get an error saying unexpected token ImagePickButton. What am I doing incorrectly? Thank you

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 17 Jan 2017, 08:05 last edited by
        #12

        Looks like you forgot some #includes, added them to my original post

        "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

        G 1 Reply Last reply 19 Jan 2017, 04:16
        0
        • V VRonin
          17 Jan 2017, 08:05

          Looks like you forgot some #includes, added them to my original post

          G Offline
          G Offline
          gabor53
          wrote on 19 Jan 2017, 04:16 last edited by
          #13

          @VRonin
          I have in the .h file:

          virtual void setEditorData(QWidget *editor, const QModelIndex &index);
          

          In the .cpp:

          void ImageDelegate::setEditorData(QWidget *editor, const QModelIndex &index)
          {
              ImagePickButton* imgPick = qobject_cast<ImagePickButton*>(editor);
              Q_ASSERT(imgPick);
              imgPick->setSelectedFile(index.data(Qt::UserRole));
          }
          

          It gives me the following error message:
          C:\Programming\Projects\Folkfriends_1_0\imagedelegate.cpp:18: error: no matching function for call to 'ImagePickButton::setSelectedFile(QVariant)'
          imgPick->setSelectedFile(index.data(Qt::UserRole));

                                                            ^
          

          C:\Programming\Projects\Folkfriends_1_0\imagedelegate.h:4: In file included from ..\Folkfriends_1_0\imagedelegate.h:4:0,
          C:\Programming\Projects\Folkfriends_1_0\imagedelegate.cpp:1: from ..\Folkfriends_1_0\imagedelegate.cpp:1:
          C:\Programming\Projects\Folkfriends_1_0\imagepickbutton.h:18: candidate: void ImagePickButton::setSelectedFile(const QString&)
          void setSelectedFile(const QString& val);
          ^
          C:\Programming\Projects\Folkfriends_1_0\imagepickbutton.h:18: note: no known conversion for argument 1 from 'QVariant' to 'const QString&'

          What should I change? Everything else builds correctly.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 19 Jan 2017, 05:04 last edited by
            #14

            @gabor53 setSelectedFile expects a QString parameter so convert the QVariant returned by data to QString.

            157

            1 Reply Last reply
            1
            • V Offline
              V Offline
              VRonin
              wrote on 19 Jan 2017, 08:01 last edited by
              #15

              easy fix imgPick->setSelectedFile(index.data(Qt::UserRole).toString());

              "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

              G 1 Reply Last reply 20 Jan 2017, 04:07
              2
              • V VRonin
                19 Jan 2017, 08:01

                easy fix imgPick->setSelectedFile(index.data(Qt::UserRole).toString());

                G Offline
                G Offline
                gabor53
                wrote on 20 Jan 2017, 04:07 last edited by
                #16

                @VRonin
                Thank you. This part works now. I added

                ImagePickButton *myImagePickButton = new ImagePickButton(this);
                myImagePickButton->show();
                

                to mydeelgate.cpp createEditor.

                When the user clicks on the image now it creates the button saying "Select Image". But this button is not on the view; it is separated from it and appears in the top left corner of the screen. How can I add it to the view so the button is on the actual image the user is trying to change? Thank you.

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 20 Jan 2017, 05:52 last edited by
                  #17

                  do not call show() just return that widget from that method. Again I'd advise you just to use the code I posted above even for the 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

                  G 1 Reply Last reply 21 Jan 2017, 03:46
                  1
                  • V VRonin
                    20 Jan 2017, 05:52

                    do not call show() just return that widget from that method. Again I'd advise you just to use the code I posted above even for the delegate

                    G Offline
                    G Offline
                    gabor53
                    wrote on 21 Jan 2017, 03:46 last edited by
                    #18

                    @VRonin
                    I definitely want to use the code you posted. Should I call the method returning the widget from myDelegate.cpp or from fixdb.cpp where the model is set up? Currently I have this:

                     ui->tableView_Fix->setItemDelegate (new myDelegate);
                    

                    This declares that the delegate will be myDelegate from now on for everything. Can this work somehow ?

                        ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate);
                        ui->tableView_Fix->setItemDelegate (new myDelegate);
                    

                    Saying that if column 3 is chosen go with ImageDelegate; all other cases myDelegate.
                    It this is the way, what is missing? Currently it opens a text window in column 4). Thank you.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 21 Jan 2017, 15:30 last edited by
                      #19

                      do it the other way around and do not leak memory:

                          ui->tableView_Fix->setItemDelegate (new myDelegate(this));
                      ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
                      
                      

                      "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

                      G 2 Replies Last reply 21 Jan 2017, 18:13
                      0
                      • V VRonin
                        21 Jan 2017, 15:30

                        do it the other way around and do not leak memory:

                            ui->tableView_Fix->setItemDelegate (new myDelegate(this));
                        ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
                        
                        
                        G Offline
                        G Offline
                        gabor53
                        wrote on 21 Jan 2017, 18:13 last edited by gabor53
                        #20
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • V VRonin
                          21 Jan 2017, 15:30

                          do it the other way around and do not leak memory:

                              ui->tableView_Fix->setItemDelegate (new myDelegate(this));
                          ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
                          
                          
                          G Offline
                          G Offline
                          gabor53
                          wrote on 26 Jan 2017, 04:31 last edited by
                          #21

                          @VRonin
                          So far I came across 2 problems.

                          1. If I add
                          ui->tableView_Fix->setItemDelegateForColumn(3, new ImageDelegate(this));
                          

                          to fixdb.cpp , nothing happens, because myDelegate.cpp evaluates which cell the user clicked on. I can add

                          return new ImagePickButton(parent);
                          

                          to myDelegate createEditor and it returns the button, but I couldn't figure out how to connect imageDelegate to the existing code. Please advise which is the best solution.

                          1. The button is about 10px smaller on the top and on the left than the original cell. How can I make it bigger to cover the size of the original cell (originally its 100 x 100). It doesn't respond to any of the size settings.
                            Thank you for your help.
                          1 Reply Last reply
                          0

                          19/21

                          21 Jan 2017, 15:30

                          • Login

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