identity id and exclusive group Radio Button created dynamically from a database



  • Hi everyone,

    I made a little app which read the possible answers of a quiz from a database.

    The first problem is that I don't know how to definite the button id if i create them dynamically so I cannot identify which buttons are pressed to save the answers in the database.

    Another problem is that I cannot create Exclusive Groups dynamically.

    I hope some you can help me. Thank you all and sorry for my bad English.

    RadioButton.qml*********************
    import QtQuick 2.4
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Window 2.0
    import "Function.js" as Function

    Page{
    id:page1
    Component.onCompleted: {Function.openDB()}

    Column{
    id:column
    anchors{
    top:parent.top
    horizontalCenter: parent.horizontalCenter
    }
    spacing:20

    Row{
        id:rowButtons
    
        Button{
            id:butR
            height:20
            width:100
            text: "READ!"
            onClicked: {read()}
        }
        Button{
            id:butI
            height:20
            width:100
            text: "SAVE ANSWERS!"
            //onClicked: {Function.insertWorkerAnswers(workerName.text, workerSurname.text)}
        }}
    Row{
        spacing:10
        Rectangle{
            id:name
            height:20
            width:50
            Text{
                anchors.fill: parent
                text:"Name"
            }
        }
        TextField{
            id:workerName
            height: name.height
            width:name.width
        }
    }
    Row{
        spacing:10
        Rectangle{
            id:surname
            height:20
            width:50
            Text{
                anchors.fill: parent
                text:"Surname"
            }
        }
        TextField{
            id:workerSurname
            height: name.height
            width:name.width
        }
    }
    

    }

    ScrollView{
    id:quiz
    anchors{
    top:column.bottom
    bottom:parent.bottom
    topMargin:10
    bottomMargin: 10
    left:parent.left
    right:parent.right
    }

    width: parent.width
    height:parent.height
    
    
    Column{
        id:quizList
        spacing:3
        anchors{
            top:parent.top
            topMargin:5
            left:parent.left
            leftMargin:10
            right:parent.right
            rightMargin: 10
            bottom: parent.bottom
            bottomMargin:1
        }}
    

    }

    Component{
    id:answerComponent
    RadioButton{
    id:answ
    property int numberQ
    property int numberA
    property string textA
    anchors{
    left:parent.left
    leftMargin: 20
    }
    text:textA
    }
    }

    function read(){
    for(var j=0;j<5;j++){
    var rrItems=Function.readQuizAnswers(j)
    for(var n in rrItems){
    newAComponent(rrItems[n])
    }

    }
    

    }

    function newAComponent(args){
    var anote=answerComponent.createObject(quizList,args)
    }
    }

    Page.qml**********
    import QtQuick 2.4
    import QtQuick.Controls 1.2
    import QtQuick.Layouts 1.1
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Window 2.0

    Rectangle {
    id: page
    height: 600
    width:page.height/1.8
    border.color: "#4c4c4c"
    gradient: Gradient {
    GradientStop {
    position: 0
    color: "#7eb06b"
    }

    GradientStop {
        position: 1
        color: "#000000"
    }
    

    }
    }

    Function.js*******************************************
    .pragma library
    .import QtQuick.LocalStorage 2.0 as Sql

    var _DB

    //CREATE**********************************************

    function openDB() {
    print("_dbase.createDB()")
    _DB = Sql.LocalStorage.openDatabaseSync("AppDataBase","1.0","The DB of the app", 1000000);
    createQuizAnswers();
    createWorkerAnswers();
    }

    function createQuizAnswers() {
    print("respostesDB.createTable()")
    _DB.transaction( function(tx) {
    tx.executeSql("CREATE TABLE IF NOT EXISTS quizAnswer(quizAnswerId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,numberQ INTEGER NOT NULL, numberA INTEGER NOT NULL,textA TEXT NOT NULL)");
    });

    deleteQuizAnswers()
    insertQuizAnswer(1,1,"Male")
    insertQuizAnswer(1,2,"Female")
    insertQuizAnswer(2,1,"<20")
    insertQuizAnswer(2,2,"<40")
    insertQuizAnswer(2,3,"<60")
    insertQuizAnswer(2,4,">60")
    insertQuizAnswer(3,1,"Europe")
    insertQuizAnswer(3,2,"North America")
    insertQuizAnswer(3,3,"South America")
    insertQuizAnswer(3,4,"Others")
    }
    function createWorkerAnswers() {
    print("respostes E createTable()")
    _DB.transaction( function(tx) {
    tx.executeSql(
    "CREATE TABLE IF NOT EXISTS workerAnswer(workerAnswerId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,workerName TEXT NOT NULL, workerSurname TEXT NOT NULL, r1_1 TEXT,r1_2 TEXT,r2_1 TEXT,r2_2 TEXT,r2_3 TEXT,r2_4 TEXT,r3_1 TEXT,r3_2 TEXT,r3_3 TEXT,r3_4 TEXT)");
    });
    }

    function deleteQuizAnswers(){
    _DB.transaction( function(tx) {
    tx.executeSql("DELETE FROM quizAnswer")
    })
    }
    function insertQuizAnswer(np,nr,texto){
    _DB.transaction(function(tx){
    tx.executeSql( "INSERT INTO quizAnswer(numberQ,numberA, textA) VALUES (?,?,?)", [np,nr,texto])
    })}

    //READ**********************************************

    function readQuizAnswers(np) {
    var rrItems = {}
    _DB.readTransaction( function(tx) {
    var rs=tx.executeSql("SELECT * FROM quizAnswer WHERE numberQ=?", [np]);
    var item
    for (var h=0; h<rs.rows.length; h++) {
    item = rs.rows.item(h)
    rrItems[item.numberA] = item;
    }
    })
    return rrItems
    }

    //INSERT*****************

    function insertWorkerAnswers(name,surname,r1_1, r1_2, r2_1, r2_2, r2_3, r2_4, r3_1, r3_2, r3_3, r3_4){
    _DB.transaction(function(tx){
    tx.executeSql( "INSERT INTO workerAnswer(workerName, workerSurname,r1_1, r1_2, r2_1, r2_2, r2_3, r2_4, r3_1, r3_2, r3_3, r3_4 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", [name,surname,r1_1, r1_2, r2_1, r2_2, r2_3, r2_4, r3_1, r3_2, r3_3, r3_4])
    })}


  • Moderators

    Hi @Pisko and Welcome,

    The first problem is that I don't know how to definite the button id if i create them dynamically so I cannot identify which buttons are pressed to save the answers in the database.

    Unfortunately you cant assign an id for dynamically created items. Check this for more details.

    Another problem is that I cannot create Exclusive Groups dynamically.

    I think you can use a ExclusiveGroup here.



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