Purpose of delegates and models



  • Hello !
    Trying to figure out how model-delegates works CONCRETELY.

    Obviously, i read tutorials and i know the basics on it but the logic appears TOTALLY illogic to me whatever how many examples and posts i read about it .

    I'm currently working with a TableView (code is probably dirty as hell because i'm trying everything i found online - i'm so open to every critics/advises/lessons/other) :
    0_1519293311383_TableView.PNG

    The corresponding QML code :

    TableView
            {
                id:tableViewReadWrite
                anchors.fill: parent
    
                property int currentColumn: 0
    
                model: tvmodel
                rowDelegate: Rectangle {color: "#fff"}
    
                itemDelegate: Rectangle {
                    color:
                    {
                        var activeRow = tableViewReadWrite.currentRow === styleData.row
                        var activeColumn = tableViewReadWrite.currentColumn === styleData.column
                        activeRow && activeColumn ? "steelblue" : "white"
                    }
                    TextInput
                    {
                        id: tiCell
                        text: styleData.value
                        font.pixelSize: 15
    
                        validator: RegExpValidator { regExp: /[\w]{8}/}
    
                        onTextEdited:
                        {
                            var currentRow = styleData.row
                            var currentColumn = styleData.column
                            var newValue = tiCell.text
    
                            if(currentColumn === 2)
                            {
                                readWrite.setAlpha(currentRow, newValue);
                            }
                        }
                    }
                    MouseArea
                    {
                        anchors.fill: parent
                        onClicked:
                        {
                            if(styleData.column !== 0)
                            {
                                var newAlphaLine = tiCell.text
                                newAlphaLine = newAlphaLine.replace(/ +/g, "");
                                tiCell.text = newAlphaLine
    
                                //Set current Row/Column TableView
                                tableViewReadWrite.currentRow = styleData.row
                                tableViewReadWrite.currentColumn = styleData.column
    
                                //Set current index model with the selected row --> useless ???
                                model.currentIndex = styleData.row
    
                                //Active focus on the specific cell + cursor position --> begin
                                tiCell.forceActiveFocus()
                                tiCell.cursorPosition = tiCell.positionAt(mouseX)
                            }
                        }
                    }
                }
    
                TableViewColumn
                {
                    title: "hexdump"
                    role: "hexdump"
                    width: 100
                    horizontalAlignment: Text.AlignLeft
                    movable: false
                }
                TableViewColumn
                {
                    title: "hexadecimal"
                    role: "hexa"
                    width: 200
                    horizontalAlignment: Text.AlignLeft
                    movable: false
                }
                TableViewColumn
                {
                    title: "alphanumeric"
                    role: "alpha"
                    width: 400
                    horizontalAlignment: Text.AlignHCenter
                    movable: false
                }
    
            }
    
            ListModel
            {
                id: tvmodel
            }
    

    Here is the points i don't get :
    View --> Display
    Model --> get data and "send" it to the view
    Delegate --> This is non-sense to me. Everyone is using it in a different way and it looks like it sometimes replace View, sometimes the model, sometimes both.

    In my code, i have itemDelegate which defines the delegate for each cell of the TableView, right ?
    So how can i make different events for each column of my TableView ?
    And if the model give data to the cells, what is the point in having a text property in my TextInput or a styleData.value ?
    Which one prime on the others ?

    In my case i want to :

    1. define a different validator for each column
    2. "link" the columns data so modifying one will change the others but i didn't find any other solution than get the data back in my class and call them again in an event.
      I tried to modify the model data using the methods set, setProperty and setData but it didn't change anything to the result.

    What on hell is the purpose of each of those elements ?
    Can someone tell me where i get something wrong ?

    Thanks a lot for your help, imma do my best to understand it properly



  • Model: contains the raw data, the same model can me used for multiple views
    View: lays out the items
    Delegate: paints and present the editor for each item in the view



  • @VRonin
    Okay so if i have my ListModel looking like that :

    tvmodel.append({hexdump: readWrite.getHexdumpLine(1), hexa: readWrite.getHexaLine(1), alpha: readWrite.getAlphaLine(1)})
                if(Card.getCardType() === "Mifare4K")
                {
                    tvmodel.append({hexdump: readWrite.getHexdumpLine(2), hexa: readWrite.getHexaLine(2), alpha: readWrite.getAlphaLine(2)})
                }
    

    And make a delegate on my TableViewColumn like that :

    TableViewColumn
                {
                    title: "alphanumeric"
                    role: "alpha"
    
                    delegate: Rectangle {
                        TextInput
                        {
                            onTextEdited:
                            {
                                [...]
                                tvmodel.set(1, { "hexa": "10"})
                            }
                        }}}
    

    Having a TableViewColumn whose role is "hexa", i should have "test" written in my column, no ?
    But i don't, so even though i know you're totally right, i'm sorry to tell you something else is missing (i also deleted the itemDelegate of TableView to be sure there wouldn't be any compatibility problems)

    I mean, i read so much pages of the doc and everyting i try to use seems to be unusable.



  • @Sillimon said in Purpose of delegates and models:

    should have "test" written in my column, no ?

    I have no idea where that "test" should come from
    can you try?

    delegate: Component{
                        TextInput
                        {
                            text: hexa
                        }}
    


  • @VRonin
    I obtain an empty cell (other columns cells work normally though)

    I got something acceptable in terms of display, i wonder if that text property in the delegate ain't actually just useless...
    Like you just set it to styleData.Value and it always take the new value ?

    I think Qt framework is sometimes so complete than some features/classes/properties become useless by Qt trying to be too versatile...

    Still remains the question of the priority between delegates, which one comes first ? ListViewColumn.delegate or TableView.ItemDelegate ?
    I will try to make some test on that...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.