单击按钮时控制qt中的循环
controlling a loop in qt on button clicks
我是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。
相关文章:
- OpenCV QT,显示视频的帧(不使用while循环)
- 具有Qt事件循环的可移植通用共享库设置
- Qt 在另一个线程中无限循环
- 与Qt同时运行后台循环或回调C++应用程序?
- Qt 多个异步循环和 qApp->processEvents();
- C /QT 5 Gstreamer继续循环
- Qt有循环缓冲吗?
- 尝试在自定义 QT 创建器文本编辑器中创建查找和替换循环.我似乎无法设置光标位置
- 如何在Qt Creator的constexpr函数中使用循环
- Qt的事件循环线程是安全的还是原子的?处理"队列连接"时如何同步?
- Qt在游戏循环中短时间内停止关键事件
- 如何将lambda函数排队到Qt的事件循环中?
- DLL中的QT事件循环
- QT "Tick"小部件循环
- 运行Qt 5.10事件循环howto的Windows服务service_WIN32_OWN_PROCESS
- Qt 停止来自 GUI 线程的工作线程循环
- 如何在 Qt 文件流中使用 >> 运算符 while 循环
- QT 不使用 while 循环进行响应
- 循环QT C 的不正确总和
- 使用 for 循环 Qt c++ 绘制圆圈