Skip to content
Snippets Groups Projects
Commit e0213c67 authored by Pouchet Pierre's avatar Pouchet Pierre
Browse files

Ajout classe Wordchooser

une instance de cette classe contiendra le mot à deviner, pour l'instant un clic sur le bouton est censé choisir un mot mais cela ne marche pas
parent 52a07286
Branches
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ qt_add_qml_module(appMotus
QML_FILES Key.qml
QML_FILES BackKey.qml
QML_FILES Bouton.qml
SOURCES wordchooser.h wordchooser.cpp
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
......
......@@ -42,7 +42,13 @@ Window {
y: 0
width: 400
height: 200
onClicked: case3._textColor="#51c3e1"
onClicked: {
let mot = wordChooser.getRandomWord();
console.log("Mot choisi :", mot);
case3._textText = mot;
onClicked: case3._textColor="#51c3e1";
}
Case {
id: case3
......
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "WordChooser.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
WordChooser wordChooser;
engine.rootContext()->setContextProperty("wordChooser", &wordChooser);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("Motus", "Main");
return app.exec();
}
#include "WordChooser.h"
#include <QFile>
#include <QTextStream>
#include <QRandomGenerator>
QFile file("words_alpha.txt");
WordChooser::WordChooser(QObject *parent) : QObject(parent) {
loadWords();
}
void WordChooser::loadWords() {
QFile file(":/words_alpha.txt"); // adapte le chemin si besoin
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (!line.isEmpty())
words.append(line);
}
file.close();
}
}
QString WordChooser::getRandomWord() {
if (words.isEmpty())
return "";
int index = QRandomGenerator::global()->bounded(words.size());
return words[index];
}
#ifndef WORDCHOOSER_H
#define WORDCHOOSER_H
#include <QObject>
#include <QString>
#include <QStringList>
class WordChooser : public QObject {
Q_OBJECT
public:
explicit WordChooser(QObject *parent = nullptr);
Q_INVOKABLE QString getRandomWord();
private:
QStringList words;
void loadWords();
};
#endif // WORDCHOOSER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment