Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InsertRow/SetRow #194

Closed
Haiyang-Bian opened this issue Mar 3, 2024 · 3 comments
Closed

InsertRow/SetRow #194

Haiyang-Bian opened this issue Mar 3, 2024 · 3 comments

Comments

@Haiyang-Bian
Copy link

When I use 'TableViewColumn' in the section of 'TableView', I got the following error: "TableViewColumn is not a type". How can I settle this promblem without changing the way to building a table?
My code is as follows:

import QtQuick
import QtQuick.Window 
import QtQuick.Controls 
import Qt.labs.qmlmodels

Window {
    visible: true
    width: 400
    height: 300
    title: "Table"

    
    ListModel {
        id: tableModel
        ListElement { name: "Alice"; age: 20; gender: "Female" }
        ListElement { name: "Bob"; age: 22; gender: "Male" }
        ListElement { name: "Charlie"; age: 21; gender: "Male" }
        ListElement { name: "David"; age: 23; gender: "Male" }
        ListElement { name: "Eve"; age: 19; gender: "Female" }
    }

    
    TableView {
        anchors.fill: parent
        model: tableModel 
        selectionMode: SelectionMode.SingleSelection 
        selectionBehavior: SelectionBehavior.SelectRows 

       
        TableViewColumn {
            role: "name" 
            title: "Name"
            width: 100 
        }

        TableViewColumn {
            role: "age"
            title: "Age"
            width: 100
        }

        TableViewColumn {
            role: "gender"
            title: "Gender"
            width: 100
        }

        
        itemDelegate: Item {
           
            Text {
                text: styleData.value 
                anchors.fill: parent
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            
            Rectangle {
                anchors.fill: parent
                color: styleData.selected ? "blue" : "white" 
            }
            
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("You clicked row " + styleData.row)
                    console.log("Name: " + model.name)
                    console.log("Age: " + model.age)
                    console.log("Gender: " + model.gender)
                }
            }
        }
    }
}
@Haiyang-Bian
Copy link
Author

How can I use the "insertRow" and "setRow" when I using a model in my component with qml? I've tried jsonObject and there dosen't have this method, I've tried Vector and got "DimensionMismatch: array could not be broadcast to match destination".
My code is as follows:

import QtQuick 2.0
import QtQuick.Controls 2.15
import QtQuick.Window

Window {
    id: window
    width: 700
    height: 700
    visible: true

    ComboBox {
        model: tList
        textRole: "name"
        valueRole: "type"
        width: 200
        height: 50
        currentIndex: 0
        onCurrentIndexChanged: {
            tList.removeRow(currentIndex)
            tList.insertRow(currentIndex,{
                name: "New Item",
                type: "New Type"
            })
        }
        delegate: ItemDelegate {
            width: parent.width
            height: 40
            contentItem: Row {
                spacing: 10
                Text {
                    text: name
                    font.pixelSize: 20
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    }
}

@Haiyang-Bian Haiyang-Bian changed the title TableViewColumn InsertRow/SetRow Mar 6, 2024
@barche
Copy link
Collaborator

barche commented Mar 6, 2024

Hi, regarding TableViewColumn: this was a QtQuick.Controls version 1 component and it is removed in Qt 6.

Inserting and removing rows with the Julia item model is shown in these examples:

There is also the tableview test that shows the full functionality, but it's a little less readable:

@Haiyang-Bian
Copy link
Author

Thanks, the second example of tableview helps me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants