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. Provide the model to a ComboBox which is a delegate inside a TableView which has already a model.
Forum Updated to NodeBB v4.3 + New Features

Provide the model to a ComboBox which is a delegate inside a TableView which has already a model.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 568 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.
  • L Offline
    L Offline
    LRDPRDX
    wrote on last edited by
    #1

    Hi, Qt'ers

    I have a TableView whose column uses a ComboBox as a delegate. I have also a model for this view (which I inherit from the QAbstractTableModel and override all necessary member-functions: data, rowCount, setData, etc.). Let's call this model as tableModel. Everything works fine for now.

    The question is about those comboboxes: the ComboBox element also have the model property which I'ld like to use.

    So this is a sketch of the code.

    //Main.qml
    /
    /imports
    
    Item {
        property var tableModel //set from C++ (for the sake of simplicity)
    
        TableView {
            id: tableView
    
            model: tableModel
    
            delegate: DelegateChooser {
                DelegateChoice {
                    column: 0
                    // CheckBox for the first column
                    delegate: CheckBox {
                        checked: model.display
                        onToggled: model.edit = checked
                    }
                }
    
                DelegateChoice {
                    column: 1
                    // ComboBox for the second column <------
                    delegate: ComboBox {
                        currentIndex: display // This value is from the tableModel
                        model: // WHAT SHOULD IT BE ?
                    }
                |
            }
        }
    }
    

    This how it looks like (the combo of interest is in the Data Type column):screenshot_05_29_15:01:50.png

    So the tableModel stores the current index only -- not all possible values available to select from.

    Please, note: The example I've shown is a demo -- the real table consists of more columns and has more different delegates.

    Of course, I could've done this:

    ...
    model: ["Numeric", "String"]
    onAccepted: edit = currentIndex
    

    but I would like to avoid the hardcoded values in QML. So what would you do in my situation, provided the model is available from C++ ?

    Thank you in advance !

    B 1 Reply Last reply
    0
    • L LRDPRDX

      Hi, Qt'ers

      I have a TableView whose column uses a ComboBox as a delegate. I have also a model for this view (which I inherit from the QAbstractTableModel and override all necessary member-functions: data, rowCount, setData, etc.). Let's call this model as tableModel. Everything works fine for now.

      The question is about those comboboxes: the ComboBox element also have the model property which I'ld like to use.

      So this is a sketch of the code.

      //Main.qml
      /
      /imports
      
      Item {
          property var tableModel //set from C++ (for the sake of simplicity)
      
          TableView {
              id: tableView
      
              model: tableModel
      
              delegate: DelegateChooser {
                  DelegateChoice {
                      column: 0
                      // CheckBox for the first column
                      delegate: CheckBox {
                          checked: model.display
                          onToggled: model.edit = checked
                      }
                  }
      
                  DelegateChoice {
                      column: 1
                      // ComboBox for the second column <------
                      delegate: ComboBox {
                          currentIndex: display // This value is from the tableModel
                          model: // WHAT SHOULD IT BE ?
                      }
                  |
              }
          }
      }
      

      This how it looks like (the combo of interest is in the Data Type column):screenshot_05_29_15:01:50.png

      So the tableModel stores the current index only -- not all possible values available to select from.

      Please, note: The example I've shown is a demo -- the real table consists of more columns and has more different delegates.

      Of course, I could've done this:

      ...
      model: ["Numeric", "String"]
      onAccepted: edit = currentIndex
      

      but I would like to avoid the hardcoded values in QML. So what would you do in my situation, provided the model is available from C++ ?

      Thank you in advance !

      B Offline
      B Offline
      Bob64
      wrote on last edited by Bob64
      #2

      @LRDPRDX your combo box needs its own model to define the drop down values. Are these values already part of your table model? Or are they at least easily accessible in the implementation of your table model? If so, one possibility is to define a new role in your model that provides the allowed values for the combo. This could be a simple string list value as ComboBox accepts a string list as a model.

      DelegateChoice {
               column: 1
               // ComboBox for the second column <------
               delegate: ComboBox {
                      currentIndex: display // This value is from the tableModel
                      model: optionValues // new role exposed by table model 
                 }
      }
      
      L 1 Reply Last reply
      0
      • B Bob64

        @LRDPRDX your combo box needs its own model to define the drop down values. Are these values already part of your table model? Or are they at least easily accessible in the implementation of your table model? If so, one possibility is to define a new role in your model that provides the allowed values for the combo. This could be a simple string list value as ComboBox accepts a string list as a model.

        DelegateChoice {
                 column: 1
                 // ComboBox for the second column <------
                 delegate: ComboBox {
                        currentIndex: display // This value is from the tableModel
                        model: optionValues // new role exposed by table model 
                   }
        }
        
        L Offline
        L Offline
        LRDPRDX
        wrote on last edited by
        #3

        @Bob64 Hmm, It sounds cool actually. Natural and simple. I will try this.

        @Bob64 said in Provide the model to a ComboBox which is a delegate inside a TableView which has already a model.:

        Or are they at least easily accessible in the implementation of your table model?

        Yes, they are accessible from the tableModel. I mean they are on the C++ side so I can easily integrate the access to them into the tableModel class.

        Thank you very much.

        1 Reply Last reply
        0
        • L LRDPRDX has marked this topic as solved on

        • Login

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