2016-04-10 11 views
1

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() 
{ 

} 
+0

質問自体に[MCVE](http://stackoverflow.com/help/mcve)を入力してください。 – derhass

+0

完了!知らせてくれてありがとうございます – CODError

答えて

1

あなたの三角形がz=-2で描かれています。あなたは投影行列を設定しないように見えるので、それはアイデンティティのままになります。そして、OpenGLは範囲の外にあるものをクリップします。[-1,1]すべての3次元に沿って(そしてどの角度を回転させても、 、原点から常に2単位離れた状態になります)。

OpenGLを学んでいるときは、使用しているGL機能のほとんどがであり、これは約10年後には廃止されましたです。最新のGLでは、これらの機能は完全に削除されています。あなたの本はひどく時代遅れです。代わりに現代のOpenGLを学ぶためのアドバイスしかできません。

関連する問題