Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm new to Qt and just to programming in general, and I'm having some issues adding a score to a game I'm making. I have a score class, obstacle class, and a game class, among others, and I'm trying to connect the QTimer currently used to spawn obstacles to update the score, so that the score goes up by 1 every 2500 milliseconds. Please help!

Here's my score header file:

#include <QGraphicsTextItem>
#include <QObject>
class score : public QGraphicsTextItem {
Q_OBJECT
public:
//constructor
score(QGraphicsItem * parent=0);
//other functions
int get_score();
int score_value;
public slots:
void increase_score();
#endif // SCORE

And here's the score source file:

//i'm including QFont, QString, QObject, and QDebug
#include "score.h"
#include "obstacle.h"
score::score(QGraphicsItem *parent) : QGraphicsTextItem(parent){
  //initialize score to zero
  score_value = 0;
  //draw the text
  setPlainText(QString("Score: ") + QString::number(score_value));
  setDefaultTextColor(Qt::white);
  setFont(QFont("arial",25));
void score::increase_score(){
  score_value++;
  qDebug() << "score has increased";
  setPlainText(QString("Score: ") + QString::number(score_value));
int score::get_score() {
 return score_value;

And here's the relevant portions of my game class source file, which creates the whole game:

#include "game.h"
#include "score.h"
#include "main_menu.h"
#include "game_over.h"
#include <QObject>
#include <QImage>
#include <QBrush>
#include <QTimer>
game::game(QWidget *parent) {
  //create the score
  Score = new score();
  scene->addItem(Score);
  //spawn obstacles
  QTimer * timer = new QTimer();
  QObject::connect(timer, SIGNAL(timeout()), copter, SLOT(spawn()));
  timer->start(2500);
  //connect score to timer
  connect(timer, SIGNAL(timeout()), Score, SLOT(increase_score()));

Right now I get errors that say

symbol(s) not found for architecture x86_64

linker command failed with exit code 1

I'm trying to use signals and slots so that the score updates instead of just sitting at zero...but I have no idea how to fix this error. Thanks for your help!

I don't think you have a good reason for adding QObject as a parent of score. QGraphicsTextItem already is a QObject. – Drew Dormann Apr 28, 2015 at 2:05 QGraphicsTextItem already inherits from QObject so you might be more breaking things with the multiple inheritance than anything. – meneldal Apr 28, 2015 at 2:07 Thanks @DrewDormann - I got rid of QObject as a parent of score but kept the macro because I define my own slot, and now I'm getting different errors: symbol(s) not found for architecture x86_64 and linker command failed with exit code 1. – portkat Apr 28, 2015 at 2:10

The signals and slots mechanism depends on extra source code generated by a Qt tool called MOC. You need to run MOC on score.h, and compile the resulting C++ source in your project. The missing symbols will be in the MOC C++ code.

If you use qmake to generate your project file it will do all this for you, but after you add new classes with signals/slots you need to run qmake again.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.