Reprendre la main sur le programme
J'ai developpé un petit conway's game of life pour un projet. Le probleme est qu'une fois que j'appuie sur Start, le programme rentre dans une boucle infinie ( jusqu'a la c'est normal, c'est moi qui l'ai codé :p ), le probleme, c'est que je ne peux plus appuyer sur le bouton close ou sur la croix, il faut que je kill le process pour arreter le programme.
Donc ma question : comment faire tourner le programme en fond tout en gardant la main pour appuyer sur les boutons ( close pour l'instant, sans doute plus d'option plus tard ).
La fonction qui boucle est game()
merci d'avance
window.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPainter>
#include "board.h"
#include "paintdevice.h"
class QPushButton;
class QLineEdit;
class QLabel;
class QImage;
class window : public QWidget
{
Q_OBJECT
public:
window(QWidget *parent = 0);
//protected:
// void paintEvent ( QPaintEvent * event );
private slots:
void game();
private:
Board *conway;
QLabel *lblCount;
QLineEdit *leditCount;
QPushButton *startButton;
QPushButton *closeButton;
int Count;
};
#endif |
window.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <QtGui>
#include <iostream>
#include "window.h"
using namespace Qt;
window::window(QWidget *parent)
:QWidget(parent) {
conway=new Board();
lblCount= new QLabel (tr("Loop Count:"));
// lblCount->move(Board::LENGTH*5 + 10,60);
leditCount = new QLineEdit;
// leditCount->move(Board::LENGTH*5 + 30,60);
startButton = new QPushButton(tr("&Start"));
startButton->setDefault(true);
// startButton->move (20,Board::LENGTH*5 + 10);
closeButton = new QPushButton(tr("&Close"));
// closeButton->move (Board::LENGTH*5 + 10,40);
connect(startButton, SIGNAL(clicked()), this, SLOT(game()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
Count = 0;
QHBoxLayout *boardLayout = new QHBoxLayout ;
boardLayout->addWidget(conway);
QVBoxLayout *topLayout = new QVBoxLayout;
topLayout->addWidget(startButton);
topLayout->addWidget(closeButton);
QHBoxLayout *bottomLayout = new QHBoxLayout ;
bottomLayout->addWidget(lblCount);
bottomLayout->addWidget(leditCount);
bottomLayout->addStretch();
QVBoxLayout *optionLayout= new QVBoxLayout;
optionLayout->addLayout(topLayout);
optionLayout->addLayout(bottomLayout);
QHBoxLayout *mainLayout= new QHBoxLayout;
mainLayout->addLayout(boardLayout);
mainLayout->addLayout(optionLayout);
setLayout(mainLayout);
setWindowTitle(tr("Conway's Game of Life"));
setFixedHeight(sizeHint().height());
}
void window::game() {
//std::cout << "I'm in" << std::endl;
while(conway->isEmpty()) {
Count+=1;
leditCount->text() = Count;
conway->nextLoop();
conway->repaint();
}
} |