1

A friend of mine and I are currently trying to make a game in C++ using Qt. Our current problem is that we need to fetch text from a QGraphicsTextItem on a button mousePressEvent. In the game menu it is possible to choose how many players there are, therefore we've placed a QGraphicsTextItem in a for-loop to make it possible for all the users to type in their names. Because of the for-loop, we don't have names for every single text item object so we can store the names. We've managed to store all the memory addresses to the objects using QMap, but we don't know how to get the text of of this. We don't even know if this is the best way to do it.

GameInfo.h

class GameInfo {
public:
    GameInfo();
    int players;

    QStringList names = (QStringList() // Default player names. This array should be overwritten by the custom names
        << "Player 1"
        << "Player 2"
        << "Player 3"
        << "Player 4"
        << "Player 5"
        << "Player 6"
        << "Player 7");

    QMap<int, QGraphicsTextItem**> textBoxMap; // This is where we store all the addresses
};

Game.cpp

    QGraphicsRectItem * overviewBox = new QGraphicsRectItem();
    overviewBox->setRect(0, 0, 782, 686);
    scene->addItem(overviewBox);

    int faceNo = 0;

// Create the player selection section
    for(int i = 1; i <= players; i++) { // "players" is defined another place in the code, and is an integer between 1 and 6
        Container * selContainer = new Container();
        selContainer->Selection(i, faceNo);
        selContainer->setPos(50, 70 + 110 * (i - 1));
        scene->addItem(selContainer);
        Container * ovContainer = new Container(overviewBox);
        ovContainer->Overview(i, faceNo);
        ovContainer->setPos(0, 0 + 110 * (i - 1));

        info->textBoxMap.insert(i, &selContainer->textBox->playerText); // This is where we save the addresses
    }

Selection.cpp

extern Game * game;

Container::Container(QGraphicsItem * parent): QGraphicsRectItem(parent) {

}

void Container::Selection(int nPlayers, int sPiceNo, QGraphicsItem *parent) {

    QString numName = QString::number(nPlayers);

    setRect(0, 0, 672, 110);
    this->setPen(Qt::NoPen); // Removes border

    int posY = this->rect().height() / 2;

    QSignalMapper * signalMapper = new QSignalMapper(this);

    arrowL = new Arrow(0, posY - 32, 0, this);
    piece = new Piece(sPiceNo, 96, posY - 32, 1, 1, this);
    arrowR = new Arrow(192, posY - 32, 1, this);
    textBox = new TextBox(game->info->names[nPlayers - 1], true, this);
    textBox->setPos(288, posY - 32);
    lockBtn = new Button("Lock", 96, 32, this);
    connect(lockBtn, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(lockBtn, nPlayers);
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(lock(int)));
    lockBtn->setPos(640, posY - 16);

}


void Container::Overview(int ovPlayers, int ovPiceNo, QGraphicsItem * parent) {
    // Some code...
}

void Container::lock(int nPlayer) {
    qDebug() << game->info->textBoxMap[nPlayer];
    qDebug() << game->info->names[nPlayer - 1];

    game->info->names[nPlayer - 1] = **game->info->textBoxMap[nPlayer].toPlainText(); // This line causes an error
}

The error that occurs because of the last line looks like this:

error: no match for 'operator=' (operand types are 'QString' and 'QGraphicsTextItem')
 game->info->names[nPlayer - 1] = **game->info->textBoxMap[nPlayer];
                                ^

TextBox.cpp

TextBox::TextBox(QString text, bool editable, QGraphicsItem * parent): QGraphicsRectItem(parent) {
    this->editable = editable;

// Draw the textbox
    setRect(0, 0, 320, 64);
    if(!editable) {
        this->setPen(Qt::NoPen); // Removes border
    }
    else if(editable) {
        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(QColor(255, 255, 255, 255));
        setBrush(brush);
    }

// Draw the text
    playerText = new QGraphicsTextItem(text, this);
    int fontId = QFontDatabase::addApplicationFont(":/fonts/built_titling_bd.ttf");

    QString family = QFontDatabase::applicationFontFamilies(fontId).at(0);
    QFont built(family, 25);
    playerText->setFont(built);
    int xPos = 0;
    int yPos = rect().height() / 2 - playerText->boundingRect().height() / 2;
    playerText->setPos(xPos,yPos);
}

My question is how do i fetch the text from the QGraphicsTextItem?

Dromnes
  • 302
  • 2
  • 3
  • 12

1 Answers1

3

You should try to learn a bit more about C++ before trying to develop a game imo. (Having public variables is against OO's and C++ paradigm)

But here is what you are looking for: http://doc.qt.io/qt-5/qgraphicstextitem.html#toPlainText

EDIT:

If you are not able to debug some line of code, I could only recommend to try to seperate your code in order to have a minimum of call in a single line. I haven't try the code bellow, but that how you should try to debug your code:

    void Container::lock(int nPlayer)
    {
       qDebug() << game->info->textBoxMap[nPlayer];
       qDebug() << game->info->names[nPlayer - 1];
       QGraphicsTextItem **value = game->info->textBoxMap.value(nPlayer, nullptr);
       game->info->names[nPlayer - 1] =(*value)->toPlainText();
    }
Nox
  • 932
  • 1
  • 9
  • 27
  • I'm sorry if I burned your eyes, that was not the purpose of this post. Anyways, I've tried your suggestion about the toPlainText(), but it didn't work either. – Dromnes Jun 22 '16 at 23:33
  • That's OK, I've edit my first version of the post, a bit too rude maybe ;). Could you explain how that didn't work please? – Nox Jun 22 '16 at 23:34
  • I don't think it was rude, just fun :) When I tried the toPlainText() I got this error: request for member 'toPlainText' in '* game->Game::info->GameInfo::textBoxMap.QMap::operator[]((*(const int*)(& nPlayer)))', which is of pointer type 'QGraphicsTextItem*' (maybe you meant to use '->' ?) game->info->names[nPlayer - 1] = **game->info->textBoxMap[nPlayer]->toPlainText(); – Dromnes Jun 22 '16 at 23:42
  • Read the error again. The answer lie within. pssssst. "maybe you meant to use '->' ?" – Nox Jun 22 '16 at 23:58
  • That's the part I don't understand. I do use the '->' before toPlainText() – Dromnes Jun 23 '16 at 00:01
  • game->info->names[nPlayer - 1] = **game->info->textBoxMap[nPlayer]->toPlainText(); – Dromnes Jun 23 '16 at 00:05
  • I have updated my answer, please tick as answered if it's what you were trying to do. – Nox Jun 23 '16 at 01:15