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. How in TableView use DelegateChooser
Forum Updated to NodeBB v4.3 + New Features

How in TableView use DelegateChooser

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 163 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.
  • M Offline
    M Offline
    Mihaill
    wrote last edited by
    #1

    Hi!
    How I can use DelegateChooser in TableView ?
    In TableView this work:

            delegate: Item {
                height: 20
                width: 40
                Rectangle {
                    anchors.fill: parent
                    color: "red"
                    border.width: 1
    
                    MLabel {
                        id: labelTableDelegateUserName
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.leftMargin: 5
                        clip: true
                        text: model.userName
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: console.log("--------------------", column)
                    }
                }
            }
    

    But my progremm don't work, when I write this:

            delegate: DelegateChooser {
                DelegateChoice {
                    column: 0
                    delegate: Item {
                        height: 20
                        width: 40
                    }
                }
    
                DelegateChoice {
                    column: 1
                    delegate: Item {
                        height: 20
                        width: 40
                    }
                }
    
                DelegateChoice {
                    column: 2
                    delegate: Item {
                        height: 20
                        width: 40
                    }
                }
    
                DelegateChoice {
                    column: 3
                    delegate: Item {
                        height: 20
                        width: 40
                    }
                }
    
                DelegateChoice {
                    column: 4
                    delegate: Item {
                        height: 20
                        width: 40
                    }
                }
            }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bandler
      wrote last edited by
      #5

      You can use ItemDelegates:

      TableView {
              id: tableView
      
              anchors.fill: parent
      
              model: TableModel {
                   TableModelColumn { display: "column 0" }
                   TableModelColumn { display: "column 1" }
      
                   rows: [
                       {
                           "column 0": "value 0",
                           "column 1": "value 1"
                       },
                       {
                           "column 0": "value 0",
                           "column 1": "value 1"
                       }
                   ]
               }
      
              delegate: DelegateChooser {
      
                  DelegateChoice {
                      column: 0;
      
                      ItemDelegate {
                          contentItem: Label {
                              text: "delegate column 0: " + model.index
                          }
                      }
                  }
      
                  DelegateChoice {
                      column: 1;
      
                      ItemDelegate {
                          contentItem: Label {
                              text: "delegate column 1: " + model.index
                          }
                      }
                  }
              }
          }
      
      M 1 Reply Last reply
      0
      • A Offline
        A Offline
        ankou29666
        wrote last edited by
        #2

        first thing that caught my attention is that as delegate you have a simple empty Item as delegate which for sure won't work. But reading again your first snippet makes me figure out that you (over)simplified it for the purpose of the post.

        second : the DelegateChoice has no delegate property, you should write something like :

        delegate: DelegateChooser // delegate property of the TableView element
        {
            DelegateChoice
            {
                column: 0
                Rectangle // use directly, not as a delegate property
                {
                     height: 20
                     width: 40
                     color: "red"
                }
            }
        }
        
        M 1 Reply Last reply
        0
        • A ankou29666

          first thing that caught my attention is that as delegate you have a simple empty Item as delegate which for sure won't work. But reading again your first snippet makes me figure out that you (over)simplified it for the purpose of the post.

          second : the DelegateChoice has no delegate property, you should write something like :

          delegate: DelegateChooser // delegate property of the TableView element
          {
              DelegateChoice
              {
                  column: 0
                  Rectangle // use directly, not as a delegate property
                  {
                       height: 20
                       width: 40
                       color: "red"
                  }
              }
          }
          
          M Offline
          M Offline
          Mihaill
          wrote last edited by Mihaill
          #3

          @ankou29666 No, this have property delegate : https://doc.qt.io/qt-6/qml-qtqml-models-delegatechoice.html
          And you code too broke my programm

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mihaill
            wrote last edited by
            #4

            It's work, but it's don't nice. Maybe somebody know how possible use DelegateChooser

                    Component {
                        id: cellColumn0
                        Rectangle {
                            anchors.fill: parent
                            color: "red"
                            border.width: 1
            
                            MLabel {
                                id: labelTableDelegateUserName
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.left: parent.left
                                anchors.right: parent.right
                                anchors.leftMargin: 5
                                clip: true
                                text: m_model.userName
                            }
                        }
                    }
            
                    Component {
                        id: cellColumn1
                        Rectangle {
                            anchors.fill: parent
                            color: "green"
                            border.width: 1
            
                            MLabel {
                                id: labelTableDelegateUserName
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.left: parent.left
                                anchors.right: parent.right
                                anchors.leftMargin: 5
                                clip: true
                                text: m_model.userName
                            }
                        }
                    }
            
                    delegate: Loader {
                        id: delegateLoader
                        sourceComponent: column === 0 ? cellColumn0 : cellColumn1
                        readonly property var m_model: model
                        readonly property int m_row: row
                        readonly property int m_column: column
                    }
            
            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bandler
              wrote last edited by
              #5

              You can use ItemDelegates:

              TableView {
                      id: tableView
              
                      anchors.fill: parent
              
                      model: TableModel {
                           TableModelColumn { display: "column 0" }
                           TableModelColumn { display: "column 1" }
              
                           rows: [
                               {
                                   "column 0": "value 0",
                                   "column 1": "value 1"
                               },
                               {
                                   "column 0": "value 0",
                                   "column 1": "value 1"
                               }
                           ]
                       }
              
                      delegate: DelegateChooser {
              
                          DelegateChoice {
                              column: 0;
              
                              ItemDelegate {
                                  contentItem: Label {
                                      text: "delegate column 0: " + model.index
                                  }
                              }
                          }
              
                          DelegateChoice {
                              column: 1;
              
                              ItemDelegate {
                                  contentItem: Label {
                                      text: "delegate column 1: " + model.index
                                  }
                              }
                          }
                      }
                  }
              
              M 1 Reply Last reply
              0
              • B Bandler

                You can use ItemDelegates:

                TableView {
                        id: tableView
                
                        anchors.fill: parent
                
                        model: TableModel {
                             TableModelColumn { display: "column 0" }
                             TableModelColumn { display: "column 1" }
                
                             rows: [
                                 {
                                     "column 0": "value 0",
                                     "column 1": "value 1"
                                 },
                                 {
                                     "column 0": "value 0",
                                     "column 1": "value 1"
                                 }
                             ]
                         }
                
                        delegate: DelegateChooser {
                
                            DelegateChoice {
                                column: 0;
                
                                ItemDelegate {
                                    contentItem: Label {
                                        text: "delegate column 0: " + model.index
                                    }
                                }
                            }
                
                            DelegateChoice {
                                column: 1;
                
                                ItemDelegate {
                                    contentItem: Label {
                                        text: "delegate column 1: " + model.index
                                    }
                                }
                            }
                        }
                    }
                
                M Offline
                M Offline
                Mihaill
                wrote last edited by
                #6

                @Bandler It's too broke my code

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mihaill
                  wrote last edited by
                  #7

                  Sorry, it work, problen in my model

                  1 Reply Last reply
                  0
                  • M Mihaill has marked this topic as solved

                  • Login

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