2016-06-22 3 views
0

내 친구와 저는 현재 Qt를 사용하여 C++로 게임을 만들려고합니다. 현재 문제는 버튼 mousePressEvent에서 QGraphicsTextItem의 텍스트를 가져와야한다는 것입니다. 게임 메뉴에서 플레이어 수를 선택할 수 있으므로 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++에 대해 좀 더 배우려고 노력해야합니다. http://doc.qt.io/qt-5/qgraphicstextitem.html#toPlainText

편집이 :

당신이 코드의 일부 라인을 디버깅 할 수없는 경우, 내가 할 수있는 당신이 찾고있는 것을

(공용 변수를 갖는 것은 OO의와 C++ 패러다임에있다) 그러나 여기 단 한 줄로 최소 통화를하려면 코드를 분리 해보는 것이 좋습니다. 나는 코드를 넣고 시도하지 않은,하지만 당신은 당신의 코드를 디버깅하려고 할 방법을 것을 :

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()를 시도했을 때이 오류가 발생했다 : 'toPlainText'멤버 'toPlainText'를 '* game-> Game :: info-> GameInfo :: textBoxMap.QMap 포인터 유형 'QGraphicsTextItem *'(어쩌면 당신은 '->'를 사용 했습니까?)을 사용하고 있습니까?). QGraphicsTextItem ('(* (const int *) (& nPlayer))) 게임 -> 정보 -> 이름 [nPlayer - 1] = ** 게임 -> 정보 -> textBoxMap [nPlayer] -> toPlainText(); – Dromnes