Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Table column cell with button image (SOLVED)
QtWS25 Last Chance

Table column cell with button image (SOLVED)

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 3.0k 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.
  • V Offline
    V Offline
    Vicky Sharma
    wrote on last edited by Vicky Sharma
    #1

    Hi to all, i am stuck in QML(Qt-Quick), if someone knows how can resolve below issue will great to me?

    I have a table-view with three columns, inside third column i used button with image on every cell of the same, when user pressed it's image source change from red to green that is fine else red.

    The problem if i am updating my model on every 1 min so before updating it get to cleared and insert again in model successfully but during this all cell's button again goes red in view which was green, so please let me know how can resolve the same?

    M 1 Reply Last reply
    0
    • V Vicky Sharma

      Hi to all, i am stuck in QML(Qt-Quick), if someone knows how can resolve below issue will great to me?

      I have a table-view with three columns, inside third column i used button with image on every cell of the same, when user pressed it's image source change from red to green that is fine else red.

      The problem if i am updating my model on every 1 min so before updating it get to cleared and insert again in model successfully but during this all cell's button again goes red in view which was green, so please let me know how can resolve the same?

      M Offline
      M Offline
      medyakovvit
      wrote on last edited by
      #2

      @Vicky-Sharma, maybe you could provide more information? What model do you use (C++, qml)? Maybe some code?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vicky Sharma
        wrote on last edited by Vicky Sharma
        #3

        This my function coming from C++ class containing jsondata

        function getData(jsonData)
        {
        var parse = JSON.parse(jsonData);
        var arrayData = parse.data;
        myModel.clear();

          for(var i = 0; i < arrayData.length; i++) {
                //in case of direct communication form client mainwindow.cpp class function
              myModel.append({"fileId": arrayData[i].fileId, "fileName": arrayData[i].fileName,
                                 "fileRating": arrayData[i].rating,"imagename": "/icons/Icons/likes5.png"});
          }      
        }
        

        //================= Table section start from here ==================

         TableView {
            id:table
            anchors.fill: parent
            alternatingRowColors: true
            horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
            backgroundVisible: true
            anchors.topMargin: parent.height/5
            anchors.bottom: parent.bottom
        
            TableViewColumn{
                id:filename;
                role: "fileName";
                resizable: false
                movable: false;
                title: "File Name";
                width: table.viewport.width/1.7
            }
        
            TableViewColumn{
                id:likeBtnColumn;
                role: "playBtn";
                resizable: false;
                movable: false;
                title: "Like ";
                width: table.viewport.width/6;
        
                delegate: Component {
        
                 Button{
                    id:playBtn;
                    width: likeBtnColumn.width/3;
        
                    Image {
                        id: image
        
                        width: playBtn.width/2
                        height: playBtn.height-2
                      source: {
                            if(myModel.get(styleData.row).fileId == 1 || myModel.get(styleData.row).fileId == 2 || myModel.get(styleData.row).fileId == 3){
                                image.source = "/icons/Icons/likes5.png";  //this is working with hard-code but not with dynamic like before if loop, for loop can use, THIS IS MY PROBLEM
                            }
                           else{
                               image.source = "/icons/Icons/likes4green.png";
                           }
                        }
        
                        fillMode: Image.PreserveAspectFit
                        x: playBtn.x + 8;
                    }
        
                    style: ButtonStyle{
                        background: Rectangle{
                        color: "transparent"
                        }
                    }
                    onClicked:{
        
                        var likeBtnPressed = myModel.get(styleData.row).fileId;
                        console.log("you pressed : " + likeBtnPressed);
                        mainWindowCalling.slotSongsLikes(likeBtnPressed);
                        alreadyLiked = likeBtnPressed;
                        image.source = "/icons/Icons/likes4green.png" ;
                        image.visible = true;
                    }
                }
              }
            }
        
        M 1 Reply Last reply
        0
        • V Vicky Sharma

          This my function coming from C++ class containing jsondata

          function getData(jsonData)
          {
          var parse = JSON.parse(jsonData);
          var arrayData = parse.data;
          myModel.clear();

            for(var i = 0; i < arrayData.length; i++) {
                  //in case of direct communication form client mainwindow.cpp class function
                myModel.append({"fileId": arrayData[i].fileId, "fileName": arrayData[i].fileName,
                                   "fileRating": arrayData[i].rating,"imagename": "/icons/Icons/likes5.png"});
            }      
          }
          

          //================= Table section start from here ==================

           TableView {
              id:table
              anchors.fill: parent
              alternatingRowColors: true
              horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
              backgroundVisible: true
              anchors.topMargin: parent.height/5
              anchors.bottom: parent.bottom
          
              TableViewColumn{
                  id:filename;
                  role: "fileName";
                  resizable: false
                  movable: false;
                  title: "File Name";
                  width: table.viewport.width/1.7
              }
          
              TableViewColumn{
                  id:likeBtnColumn;
                  role: "playBtn";
                  resizable: false;
                  movable: false;
                  title: "Like ";
                  width: table.viewport.width/6;
          
                  delegate: Component {
          
                   Button{
                      id:playBtn;
                      width: likeBtnColumn.width/3;
          
                      Image {
                          id: image
          
                          width: playBtn.width/2
                          height: playBtn.height-2
                        source: {
                              if(myModel.get(styleData.row).fileId == 1 || myModel.get(styleData.row).fileId == 2 || myModel.get(styleData.row).fileId == 3){
                                  image.source = "/icons/Icons/likes5.png";  //this is working with hard-code but not with dynamic like before if loop, for loop can use, THIS IS MY PROBLEM
                              }
                             else{
                                 image.source = "/icons/Icons/likes4green.png";
                             }
                          }
          
                          fillMode: Image.PreserveAspectFit
                          x: playBtn.x + 8;
                      }
          
                      style: ButtonStyle{
                          background: Rectangle{
                          color: "transparent"
                          }
                      }
                      onClicked:{
          
                          var likeBtnPressed = myModel.get(styleData.row).fileId;
                          console.log("you pressed : " + likeBtnPressed);
                          mainWindowCalling.slotSongsLikes(likeBtnPressed);
                          alreadyLiked = likeBtnPressed;
                          image.source = "/icons/Icons/likes4green.png" ;
                          image.visible = true;
                      }
                  }
                }
              }
          
          M Offline
          M Offline
          medyakovvit
          wrote on last edited by
          #4

          @Vicky-Sharma
          The main point is that you need to save model after changes. If i correctly understand, you load model from json every 1 min. So if you not change the value of "imagename" in json, you will receive same value every time.

          delegate: Component {
          
                   Button{
                      id: playBtn;
                      width: likeBtnColumn.width/3;
          
                      Image {
                          id: image
          
                          width: playBtn.width/2
                          height: playBtn.height-2
                          source: imageName // from the model. Is it working?
          
                          fillMode: Image.PreserveAspectFit
                          x: playBtn.x + 8;
                      }
          
                      style: ButtonStyle{
                          background: Rectangle{
                          color: "transparent"
                          }
                      }
                      onClicked:{
          
                          var likeBtnPressed = myModel.get(styleData.row).fileId;
                          console.log("you pressed : " + likeBtnPressed);
                          mainWindowCalling.slotSongsLikes(likeBtnPressed);
                          alreadyLiked = likeBtnPressed;
                          //image.source = "/icons/Icons/likes4green.png" ;
                          imageName = "/icons/Icons/likes4green.png"; // change image in model, not only in view
                          // Right now you need to save model, because you change some  data
                          image.visible = true;
                      }
                  }
          

          By the way, if you use only 2 images("/icons/Icons/likes5.png", "/icons/Icons/likes4green.png"), maybe it's better to use bool value in json and model instead of string and load either of them.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vicky Sharma
            wrote on last edited by
            #5

            Sorry for my mistake, json not sending any image source.

            function getData(jsonData)
            {
              var parse = JSON.parse(jsonData);
              var arrayData = parse.data;
              myModel.clear();
            
              for(var i = 0; i < arrayData.length; i++) {
                    //in case of direct communication form client mainwindow.cpp class function
                  myModel.append({"fileId": arrayData[i].fileId, "fileName": arrayData[i].fileName,
                                     "fileRating": arrayData[i].rating});
              }      
            }
            

            it is created by myself at the time of filling data into model.

            M 1 Reply Last reply
            0
            • V Vicky Sharma

              Sorry for my mistake, json not sending any image source.

              function getData(jsonData)
              {
                var parse = JSON.parse(jsonData);
                var arrayData = parse.data;
                myModel.clear();
              
                for(var i = 0; i < arrayData.length; i++) {
                      //in case of direct communication form client mainwindow.cpp class function
                    myModel.append({"fileId": arrayData[i].fileId, "fileName": arrayData[i].fileName,
                                       "fileRating": arrayData[i].rating});
                }      
              }
              

              it is created by myself at the time of filling data into model.

              M Offline
              M Offline
              medyakovvit
              wrote on last edited by
              #6

              @Vicky-Sharma So, your problem is solved?

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vicky Sharma
                wrote on last edited by p3c0
                #7

                Thanks for reply and i did something different in guidance of my Senior

                What i did. i did following

                In json, We add another filled "songlikedid" which is coming from database table and than match if database table (Songs, AlreadyLiked) are same than set green.png else blue

                function getData(jsonData)
                    {
                      var parse = JSON.parse(jsonData);
                      var arrayData = parse.data;
                      myModel.clear();
                
                      for(var i = 0; i < arrayData.length; i++) {
                          myModel.append({"fileId": arrayData[i].fileId, "fileName": arrayData[i].fileName,
                                             "fileRating": arrayData[i].rating, "songlikedid": arrayData[i].songlikedid});
                      }      
                    }
                
                 Button{
                                id:playBtn;
                                width: likeBtnColumn.width/3;
                
                                Image {
                                    id: image
                
                                    width: playBtn.width/2
                                    height: playBtn.height-2
                                    source: {
                                        if(myModel.get(styleData.row).songlikedid == myModel.get(styleData.row).fileId ){
                                            image.source = "/icons/Icons/likes4green.png";
                                        }
                                        else{
                                            image.source = "/icons/Icons/likes5.png";
                                        }
                                    }
                
                                    fillMode: Image.PreserveAspectFit
                                    x: playBtn.x + 8;
                                }
                
                                style: ButtonStyle{
                                    background: Rectangle{
                                    color: "transparent"
                                    }
                                }
                                onClicked:{
                
                                    var likeBtnPressed = myModel.get(styleData.row).fileId;
                                    console.log("you pressed : " + likeBtnPressed);
                                    mainWindowCalling.slotSongsLikes(likeBtnPressed);
                                    alreadyLiked = likeBtnPressed;
                                    image.source = "/icons/Icons/likes4green.png" ;
                                    image.visible = true;
                                }
                            }
                
                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