Kugadzira app yeUbuntu Kubata [QML] - [Chikamu 2]

Nhanganyaya:

Zvakanaka pano tine chinyorwa chinotevera iyo chikamu chimweMune ino isu ticha tarisa pakubata iyo data (mibvunzo), uye isu tichatanga nechikamu chine musoro chemutambo, mune inotevera tinopedzisa pfungwa nekushandisa iyo nguva uye nesarudzo nhatu.

Unogona kuwana iyo kodhi mune yangu Launchpad repository (tinya apa), kwandinozo gadziridza ini pandinoenda.

Dhatabhesi:

Nezve database yatinoshandisa U1db, iri dhatabhesi iyo inochengetedza iyo data mune fomati JSON.

Chekutanga pane zvese ngatione kuti ndeipi dhata yatinofanira kuchengetedza, isu tine mubvunzo mumwe (kunze kwenyaya yemubvunzo zvingave zvakanaka kuva nechiziviso), iine mhinduro dzinogona kuita, idzo chete 1 dzinogona kuve, saka gwaro iri JSON mubvunzo mumwe chete unofanirwa kunge uri:

{"id": 1, "mubvunzo": "pachave ne dev.fromlinux.net", "mhinduro": {r1: "hongu", r2: "kwete", r3: "hazvizivikanwe", r4: "iyi mhinduro yenhema"}, "ndizvo": 3}

Sezvo isu tichiona isu takarongeka mune fomati JSON patine chinongedzo, tambo ine mubvunzo uye r1, r2 inomiririra mhinduro 1 uye mhinduro 2, pakupedzisira tichava neimwe yemhinduro iri chaiyo.

Ngatione zvishoma mashandiro anoita U1db, chinhu chekutanga chatinofanira kuita kushandisa U1db kupinza module.

import U1db 1.0 as U1db

Isu tinozivisa dhatabhesi:

U1db.Database {
id: aDatabase
path: "aU1DbDatabase"
}

Dhatabhesi dzichachengetedzwa seU1DbDatabase.u1db, ikozvino taumba gwaro:

U1db.Document {
id: aDocument
database: aDatabase
docId: 'helloworld'
create: true
defaults: { "hello": "Hello World!" }
}

Kwatinogona kuona fomati yacho Tine iyo U1db.Query chinhu.

Tinogona kuwana Tutorial pane webhusaiti yekuvandudza ye Ubuntu.

Ngatiendei kuchikumbiro chedu uye tigadzire dhatabhesi redu, sezvataona isu tichitsanangura dhatabhesi:

U1db.Database {id: db mibvunzo; nzira: "questionsdb.u1db"}

Zvakanaka, ikozvino ngatiisei zvakasarudzika zvinhu mudhatabhesi gwaro mibvunzodb:

    U1db.Document {id: aDocument database: maswalidb docId: 'mibvunzo' gadzira: zvechokwadi zvisizvo: {"mibvunzo": [{"mubvunzo": "Will dev.fromlinux.net?", "R1": "hongu", " r2 ":" no "," r3 ":" nose "," r4 ":" false false "," correct ":" 3 "}, {" question ":" Who is hagrid (harry potter)? "," r1 ":" Mutevedzeri wemukuru wechikoro "," r2 ":" Ranger "," r3 ":" Mudzidzisi weshanduko "," r4 ":" A prefect "," correct ":" 2 "}, {"mubvunzo": "Chii guta guru reIceland?", "r1": "Amsterdam", "r2": "Reykjavik", "r3": "Tbilisi", "r4": "Almaty", "chakarurama" : "2"}]}}

Ndinoziva kuti kodhi haina kuratidzwa sezvayaifanira, asi kana tikatarisa tichaona kuti tine chinhu mabvunziro, iyo ne »[]» isu tinodudzira kuti panogona kuve nezvakawanda (hazvisi static) zvinhu; mune ino kesi tichava ne4 nekutadza, mibvunzo mina nemhinduro dzadzo dzakasiyana.

Kuti uwane zvinyorwa zvegwaro JSON, tinogona kudoma chinongedzo chegwaro ratakagadzira (rinova gwaro). Ngatimbofungidzirai kuti isu tinoda kutora iwo mavara emubvunzo wekutanga:

aDocument.contents.questions [0] .mubvunzo

Uyu mutsetse wekodhi unodzosera zvemukati zvemubvunzo zvegwaro aDocument, yechinhu 0 (yekutanga indekisi), inova iyo: "Pachave ne dev.fromlinux.net?". Kana isu tikanyora nhamba 1, zvino inoenda kumubvunzo wechipiri.

Kugadziridza pfungwa yemutambo

Zvakanaka, ikozvino sezvo isu tava kuziva kubata zvishoma dhatabhesi uko kwatakachengeta mibvunzo, isu tichaenda kuronga pfungwa yemutambo. Chekutanga pane zvese isu tichagadzira faira JavaScript (.js): Wedzera nyowani -> Qt -> Js Faira. Ndichamufonera logic.js.

Mune iyo faira isu tiri kuzogadzira akati wandei mashandiro, kuendesa iyo js faira mune yedu qml gwaro:

import "logica.js" as Logica

Isu tinosanganisa zita rekuti Logica, saka kana tichida kudaidza basa tichariita seLogica.funcion (), sekunge chiri chinhu chine nzira dzacho.

Parizvino, kuitira kuti posvo irege kureba, isu tinosiya nguva yemubvunzo uye nesarudzo dzekuzotevera, isu tichaisa pfungwa pamibvunzo nemapoinzi, ngatione chiitiko chatichazoshandisa kupfuurira kubva mubvunzo:

shanda nextQuestion (num) {// time = 0 question.text = aDocument.contents.questions [num] .question; resp1.text = aDocument.contents.questions [num] .r1; resp2.text = aDocument.contents.questions [num] .r2; resp3.text = aDocument.contents.questions [num] .r3; resp4.text = aDocument.contents.questions [num] .r4; }

Sekuona kwedu, tinopfuura nhamba yemubvunzo patiri sekupokana uye kubva pano inoisa mhinduro kumabhatani uye nemubvunzo. Ngatione izvozvi kuti tione kana mhinduro yakasarudzwa iri yechokwadi:

basa rova ​​(num, sarudzo) {var hit = false; var num_correcto = aDocument.contents.questions [num] .correcto; kana (sarudzo == chaiyo_num) hit = ichokwadi; dzoka hit; }

Iyo kodhi inotaura pachayo, kana sarudzo yakasarudzwa yakafanana neiya yakapihwa nedhatabhesi, zvino chichava chokwadi, zvikasadaro hazvizoiti. Izvozvi sezvo isu tave nemabasa aya akatsanangurwa, isu tinozoaisa mune yedu qml kodhi.

Chekutanga pane zvese tichawedzera musiyano unodzora kuti ndeupi mubvunzo watiri, tichaidaidza kuti num; Tichavawo nemamwe matatu akasiyana: kurova, kupotsa uye mapoinzi

    chivakwa int num: 0 chivakwa int zvisizvo: 0 chivakwa int kuberekwa: 0 chivakwa int npoints: 0

Pakutanga ivo vese vari pa0.

Iye zvino tinowedzera onClick kune ese mabhatani ekupindura. onClick, sekureva kwaro zita, chiitiko chichaitika kana bhatani radzvanywa:

            Bhatani {id: resp1 chinyorwa: "Mhinduro 1" anchors.horizontalCenter: parent.horizontalCenter upamhi: parent.width - 20 paClick: Logic.nextPregunta (num) npoints = npoints + makumi mashanu ekuberekwa = kuzvarwa + 1} zvimwe {nfaults = nfaults + 1}}}

Isu tinoona kuti nekudzvanya bhatani, rinotarisa kuti rakarurama here kana kuti kwete, kana riri rechokwadi, wedzera mapoinzi uye pasa mubvunzo, zvikasadaro hupenyu humwe hunosara.

Chekupedzisira tichaita wekutanga kubvunza mutoro kana Peji chikamu chikatakura:

    Peji {id: pejiGame Component.onCompleted: {Logic.nextPregunta (0)}

Ngatione kuti inoratidzika sei:

bvunzo


Izvo zviri muchinyorwa zvinoomerera pamisimboti yedu ye tsika dzekunyora. Kuti utaure chikanganiso tinya pano.

Makomendi gumi, siya zvako

Siya yako yekutaura

Your kero e havazobvumirwi ichibudiswa.

*

*

  1. Inotarisira iyo data: Miguel Ángel Gatón
  2. Chinangwa cheiyo data: Kudzora SPAM, manejimendi manejimendi.
  3. Legitimation: Kubvuma kwako
  4. Kutaurirana kwedata
  5. Dhata yekuchengetedza: Dhatabhesi inobatwa neOccentus Networks (EU)
  6. Kodzero: Panguva ipi neipi iwe unogona kudzora, kupora uye kudzima ruzivo rwako

  1.   Gabielus akadaro

    Ini ndinotevera matanho uye zvese zvinoshanda. Kungobatana ndinoti:
    * Hazvichinje kukosha kwekurova uye kupotsa. Iwe unofanirwa kuwedzera izvo kune yega bhatani mune yako yekudzvanya chiitiko
    * Uye izvo zviri mumutsara:
    kana (Logic.acerto (num, 1)) {
    chinja iyo 1 kuve nhamba yebhatani / mhinduro.

    lolbimbo, ndinoona mupiro wako uchifadza. Ndinovimba tutos nyowani !!!

    Kutenda nekugovana.

    1.    lolbimbo akadaro

      Ndinoda kuti iwe wataura pamusoro pemaonero aya nekuti handina kuaburitsa pachena pambiru, zvimwe, ini handirangarire iwo mapoinzi, kubudirira uye kukundikana, kana ukazvitarisa uchaona kuti patinodzvanya bhatani, isu tinowedzera iwo mapoinzi kubudirira ...

      Chitaera {
      id: kukundikana
      chinyorwa: «Zvikanganiso:» + nfaults
      ruvara: "tsvuku"
      }

      Chitaera {
      id: hits
      chinyorwa: «Hits:» + akaberekwa
      }

      Chitaera {
      id: mapoinzi
      zvinyorwa: «Points:» + npoints
      fontSize: "pakati"
      }

      Ini ndichajekesawo muchikamu 3.