2016-06-22 3 views
0

私の友人と私は現在Qtを使ってC++でゲームを作ろうとしています。現在の問題は、ボタンmousePressEventQGraphicsTextItemからテキストを取得する必要があることです。ゲームメニューでは、プレイヤーの数を選ぶことができるので、forループにQGraphicsTextItemを入れて、すべてのユーザーが自分の名前を入力できるようにしました。 for-loopのため、すべての単一のテキスト項目オブジェクトに名前がなく、名前を格納できます。私たちはQMapを使ってすべてのメモリアドレスをオブジェクトに格納することに成功しましたが、このテキストの入手方法はわかりません。これが最良の方法であるかどうかはわかりません。名前のないQGraphicsTextItemからテキストを取得

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 
}; 

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 
} 

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 
    } 
ために最後の行で発生するエラーは、次のようになります。

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

私の質問は、QGraphicsTextItemからテキストを取得する方法です。

答えて

2

ゲームの開発を試みる前に、C++についてもう少し学びましょう。 (パブリック変数を持つことがOOのに反しているとC++のパラダイム)

しかし、ここであなたが探しているものです。 http://doc.qt.io/qt-5/qgraphicstextitem.html#toPlainText

EDIT:

あなたは、コードのいくつかの行をデバッグすることができない場合は、私ができました1行で最小限の通話を行うために、コードを分離することをお勧めします。私は、コード怒鳴るを試していますが、あなたのコードをデバッグしようとする必要があり、どのようにしていない:

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(); 
    } 
+0

私は私があなたの目を燃やした場合、それは、この記事の目的ではありませんでしたごめんなさい。とにかく、私はtoPlainText()についてあなたの提案を試みましたが、それはどちらもうまくいきませんでした。 – Dromnes

+0

それはいいです、私はポストの最初のバージョンを編集しました、あまりにも失礼かもしれません;)。 どうしたらうまくいかないのか説明できますか? – Nox

+0

私はtoPlainText()を試したときに、このエラーが発生しました: '* game-> Game :: info-> GameInfo :: textBoxMap.QMapの' toPlainText 'メンバーのリクエストポインタタイプ 'QGraphicsTextItem *'(多分あなたは ' - >'を使用することを意図していましたか?)を使用していますか?)QGraphicsTextItem( '*(const int *)(&nPlayer)))ゲーム - >情報 - >名前[nPlayer - 1] = **ゲーム - >情報 - >テキストボックスマップ[nPlayer] - > toPlainText(); – Dromnes

関連する問題