Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:47:11+00:00 2026-06-05T19:47:11+00:00

I am trying to learn Qt and I am attempting to do so by

  • 0

I am trying to learn Qt and I am attempting to do so by making a little titres game. Currently I have a 2d array which represents the game board.

Every second this 2d array is changed by a thread (representing the passage of time) and then this thread emits a signal telling the main GUI to update based on the new game board.

My Thread is as follows:

gamethread.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

gamethread.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);
}

and the main UI which should receive the signal and update based on the new board is:

tetris.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

tetris.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();
}

When I run this I get:

QObject::connect: Cannot queue arguments of type ‘int[20][10]’
(Make sure ‘int[20][10]’ is registered using qRegisterMetaType().)

I have looked into qRegisterMetaType and Q_DECLARE_METATYPE but I am not even remotely sure how to use them or even if I must use them. Can someone give the QT newbie some assistance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:47:13+00:00Added an answer on June 5, 2026 at 7:47 pm

    You can wrap the board data in a class. It won’t work if you merely typedef’d it, since Qt will try to use non-array operator new to create instances of board data. The compiler will detect it and rightfully complain.

    It’s bad style to derive from QThread like you are doing and use it as a generic QObject. QThread is conceptually a thread controller, not a thread itself. See this answer for the idiomatic way to do it. Your GameThread should be a QObject, not a QThread.

    So:

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn python and I'm attempting a hangman game. But when I
Currently, I'm trying to learn Javascript by writing my code in Gedit then attempting
Greetings from some noob trying to learn JQuery, I am attempting to make it
Trying to learn about php's arrays today. I have a set of arrays like
Still trying to learn the basics of MVC. I'm making use of Zend_Loader for
I am attempting to learn debugging in x86 assembly and am trying to debug
I am trying learn more about JSP expression evaluations. How and which variables are
trying to learn and practice arrays but I have a problem with this small
I trying to learn web application development. I'm currently using Google App Engine to
I am attempting to learn the PyMT library and am trying to implement a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.