Qtが新しく、QPainterを使用してQWidgetを更新するループを実行しようとしています。要するに、私は、ボタンのクリックでQWidgetをリフレッシュする無限ループを実行しようとしています(開始ゲーム)。次に、別のボタンをクリックしてループを止めたいとします(エンドゲーム)。私はこの機能を得るためのより良いアプローチについても聞きたいと思います。Qtのループをボタンのクリックで制御する
私のMainWindowクラスでは、クラスGameのオブジェクトを含むスレッドを開始したいと考えています。 game-> start_game()メソッドが呼び出されると、無限ループが始まります。私はボタンをクリックしてループを開始したい。その後、ループは別のボタン「end」のクリックで終了するはずです。
//main_wid is the central widget of my MainWindow
QPushButton* btn_start = new QPushButton("start", main_wid);
QPushButton* btn_end = new QPushButton("end", main_wid);
//thread, game are private variables in my MainComponent class
thread = new QThread;
game = new Game(10);
game->moveToThread(thread);
//on btn_start click, the thread start in run_thread() method
connect(btn_start, SIGNAL(clicked(bool)), this, SLOT(run_thread()));
//on thread->start(), I call start_game() slot in the Game class which runs an infinite loop
connect(thread, SIGNAL(started()), game, SLOT(start_game()));
//here i want to connect clicked(bool) of btn_end to a method in game class
//such that i can break the loop in start_game() method.
//.......??
マイゲームクラス:ゲームクラスメソッドの
class Game : public QObject
{
Q_OBJECT
public:
Game(int n);
~Game();
public slots:
void start_game();
void end_game();
signals:
void finish_game();
private:
int num;
};
定義:
Game::Game(int n)
{
num = n;
}
void Game::start_game()
{
int i = 0;
while(true)
{
cout << "game loop started:" << i++ << endl;
}
//emit finish_thread();
}
void Game::end_game()
{
cout << "*************************end**************************" << endl;
emit finish_game();
}
Game::~Game()
{
}
質問自体に[MCVE](http://stackoverflow.com/help/mcve)を入力してください。 – derhass
完了!知らせてくれてありがとうございます – CODError