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. identity id and exclusive group Radio Button created dynamically from a database
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
databasedynamicdynamic propertcreateobject
2 Posts 2 Posters 1.6k Views 2 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.
  • P Offline
    P Offline
    Pisko
    wrote on last edited by
    #1

    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])
    })}

    p3c0P 1 Reply Last reply
    0
    • P Pisko

      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])
      })}

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0

      • Login

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