c++ - Qt:将包含二维数组的信号从一个线程传递到另一个线程

标签 c++ multithreading qt signals

我正在尝试学习 Qt,并且我正在尝试通过制作一个小滴定游戏来做到这一点。目前我有一个代表游戏板的二维数组。

每秒这个二维数组都会被一个线程改变(代表时间的流逝),然后这个线程发出一个信号告诉主 GUI 根据新的游戏板进行更新。

我的线程如下:

游戏线程.h

#ifndef GAMETHREAD_H
#define GAMETHREAD_H
#include <QtCore>
#include <QThread>
#include<QMetaType>

class GameThread : public QThread
{
    Q_OBJECT

    public:
        explicit GameThread(QObject *parent = 0);
        void run();

    private:
        int board[20][10]; //[width][height]
        void reset();

    signals:
        void TimeStep(int board[20][10]);
};

#endif // GAMETHREAD_H

游戏线程.cpp

#include "gamethread.h"
#include <QtCore>
#include <QtDebug>

//Game Managment
GameThread::GameThread(QObject *parent) :
    QThread(parent)
{
    reset();
}

void GameThread::reset()
{
    ...
}

//Running The Game
void GameThread::run()
{
    //Do Some Stuff
    emit TimeStep(board);
}

应该接收信号并根据新板更新的主 UI 是:

俄罗斯方 block .h

#ifndef TETRIS_H
#define TETRIS_H

#include <QMainWindow>
#include "gamethread.h"

namespace Ui{
    class Tetris;
}

class Tetris : public QMainWindow
{
    Q_OBJECT

    public:
        explicit Tetris(QWidget *parent = 0);
        ~Tetris();
        GameThread *mainThread;

    private:
        Ui::Tetris *ui;

    private slots:
        int on_action_Quit_activated();
        void on_action_NewGame_triggered();

    public slots:
        void onTimeStep(int board[20][10]);


};

#endif // TETRIS_H

俄罗斯方 block .cpp

#include <QMessageBox>
#include <QtGui>
#include <boost/lexical_cast.hpp>
#include <string>

#include "tetris.h"
#include "ui_tetris.h"

Tetris::Tetris(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Tetris)
{
    ui->setupUi(this);
    mainThread = new GameThread(this);

    connect(mainThread, SIGNAL(TimeStep(int[20][10])),
            this, SLOT(onTimeStep(int[20][10])),
            Qt::QueuedConnection);
}

Tetris::~Tetris()
{
    delete ui;
}

void Tetris::onTimeStep(int board[20][10])
{
    //receive new board update my display
}

void Tetris::on_action_NewGame_triggered()
{
    mainThread->start();
}

当我运行它时,我得到:

QObject::connect:无法对“int[20][10]”类型的参数进行排队 (确保使用 qRegisterMetaType() 注册了“int[20][10]”。)

我已经研究过 qRegisterMetaType 和 Q_DECLARE_METATYPE,但我什至不确定如何使用它们,甚至不确定我是否必须使用它们。有人可以给QT新手一些帮助吗?

最佳答案

您可以将板数据包装在一个类中。如果您只是对其进行 typedef,它将无法工作,因为 Qt 将尝试使用非数组运算符 new 来创建板数据的实例。编译器会检测到它并正确地提示。

像您这样从 QThread 派生并将其用作通用 QObject 是一种糟糕的风格。 QThread 在概念上是一个线程 Controller ,而不是线程本身。参见 this answer对于惯用的方式来做到这一点。您的 GameThread 应该是 QObject,而不是 QThread。

所以:

struct Board {
  int data[20][10];
}
Q_DECLARE_METATYPE(Board);

int main(int argc, char ** argv)
{
   QApplication app(argc, argv);
   qRegisterMetatype<Board>("Board");

   ...

   Game * game = new Game;
   QThread thread;
   game->connect(&thread, SIGNAL(started()), SLOT(start());
   game->connect(game, SIGNAL(finished()), SLOT(deleteLater()));
   thread.connect(&game, SIGNAL(finished()), SLOT(quit());
   game.moveToThread(&thread);

   thread.start(); // you can start the thread later of course
   return app.exec();
}

class Game: public QObject
{
QTimer timer;
Board board;
public slots:
   void start() {
     connect(&timer, SIGNAL(timeout()), SLOT(tick()));
     timer.start(1000); // fire every second
   }
   void finish() {
     timer.stop();
     emit finished();
   }
protected slots:
   void tick() {
      ... // do some computations that may take a while
      emit newBoard(board);
      // Note: it probably doesn't apply to trivial computations in
      // a Tetris game, but if the computations take long and variable
      // time, it's better to emit the board at the beginning of tick().
      // That way the new board signal is always synchronized to the timer.
   }  
signals:
   void newBoard(const Board &);
   void finished();
}

关于c++ - Qt:将包含二维数组的信号从一个线程传递到另一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039594/

相关文章:

c++ - 插入 vector

C++ 生成警告 : dereferencing type-punned pointer will break strict-aliasing rules

c++ - 接收右值并调用左值版本的函数的无限递归

python - 使用信号和槽在两个小部件之间发送消息

c++ - 可以用什么代替 QRectF?

c++ - XCode 4.5 'tr1/type_traits' 找不到文件

java - java 中 100% 竞争自由代码的同步替代方案

java - 从同一个循环中延迟调用不同的方法

C# - 如何唤醒休眠线程?

c++ - 反复点击QSplashscreen背景变黑