From: Emmanuel Granatello Date: Mon, 1 Mar 2010 14:34:25 +0000 (+0100) Subject: Inserita la QwaitCondition X-Git-Url: http://git.maemo.org/git/?p=qmemory;a=commitdiff_plain;h=44f2521902036db1caced5a4f418e78ef3a686ab;hp=2c930692d1472c797cae05f6f942c3a39faabef8 Inserita la QwaitCondition Per risolvere alcuni problemi relativi al thread sono state agginte le QMutex e le QWaitCondition --- diff --git a/Memory.pro b/Memory.pro index dd0bef6..2746a86 100644 --- a/Memory.pro +++ b/Memory.pro @@ -2,7 +2,7 @@ # Project created by QtCreator 2010-02-18T12:16:41 # ------------------------------------------------- QT += svg -TARGET = Memory +TARGET = qmemory TEMPLATE = app RESOURCES = qled.qrc SOURCES += main.cpp \ diff --git a/mainwindow.cpp b/mainwindow.cpp index 70fd57c..3bdfb6d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,8 +9,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - bestScore=0; - readSettings(); + bestScore=0; + readSettings(); ui->setupUi(this); statusLed = new QLed(ui->verticalLayoutWidget); ui->horizontalLayout_4->insertWidget(0,statusLed); @@ -75,8 +75,11 @@ void MainWindow::sbagliato(int score){ sscore.append("\n Best score: "); sscore.append(QString::number(bestScore)); QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape); - //tread.genera(); - exit(0); + bestScore=0; + readSettings(); + tread.genera(); + + //exit(0); } void MainWindow::disableAll(){ diff --git a/testThread.cpp b/testThread.cpp index fcdf34a..e3087e1 100644 --- a/testThread.cpp +++ b/testThread.cpp @@ -5,7 +5,7 @@ #include TestThread::TestThread(QObject *parent):QThread(parent){ colore=0; - attendiInputUtente=false; + index=0; _score=0; @@ -18,7 +18,7 @@ TestThread::~TestThread(){ void TestThread::run(){ sleep(1); while(true){ - + mutex.lock(); qsrand(QDateTime::currentDateTime().toTime_t()); sequenza.append(qrand()%9); int i=0; @@ -35,12 +35,11 @@ void TestThread::run(){ i++; } i=0; - attendiInputUtente=true; emit enableInterface(); + attendiInputUtente.wait(&mutex); + + mutex.unlock(); - while(attendiInputUtente){ - msleep(1); - } } @@ -48,7 +47,7 @@ void TestThread::run(){ void TestThread::updateCaption(int inputUser){ - if(attendiInputUtente){ + if(sequenza.at(index)==inputUser) { @@ -57,7 +56,7 @@ void TestThread::updateCaption(int inputUser){ emit score(_score); if(index==sequenza.size()){ sleep(1); - attendiInputUtente=false; + attendiInputUtente.wakeAll(); index=0; } } @@ -66,14 +65,14 @@ void TestThread::updateCaption(int inputUser){ _score=0; sequenza.clear(); index=0; - attendiInputUtente=true; + attendiInputUtente.wakeAll(); } - } + } void TestThread::genera() { - attendiInputUtente=false; + attendiInputUtente.wakeAll(); } diff --git a/testThread.h b/testThread.h index fdb6855..b5ad416 100644 --- a/testThread.h +++ b/testThread.h @@ -2,6 +2,8 @@ #define TESTTHREAD_H #include +#include +#include class TestThread : public QThread @@ -26,10 +28,11 @@ public slots: private: + QWaitCondition attendiInputUtente; int colore; int index; int _score; - bool attendiInputUtente; + QMutex mutex; QList sequenza; }; #endif // TESTTHREAD_H