单击按钮时控制qt中的循环

controlling a loop in qt on button clicks

本文关键字:循环 qt 控制 按钮 单击      更新时间:2023-10-16

我是Qt的新手,我正在尝试运行一个循环,该循环将使用QPainter更新QWidget。简而言之,我正试图运行一个无限循环,在点击按钮时刷新QWidget(比如开始游戏)。然后我想在点击另一个按钮时停止循环(比如结束游戏)。我还想了解获得此功能的任何更好的方法。

在我的MainWindow类中,我想启动一个包含类Game对象的线程。当调用game->start_game()方法时,一个无限循环开始。我想点击按钮开始循环。然后单击另一个按钮"结束",循环应该退出。

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

您的三角形是在z=-2绘制的。由于您似乎从未设置过投影矩阵,因此它将保持不变,OpenGL将沿所有三维剪裁位于[-1,1]范围之外的任何对象(无论旋转角度如何,由于它将围绕原点旋转,因此它始终与原点保持2个单位的距离)。

当您学习OpenGL时,您应该意识到,您使用的大多数GL功能已被弃用近十年了。在现代GL中,这些功能被完全去除。你的书似乎过时得可怕。我只能建议学习现代OpenGL。