- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我和我的一个 friend 目前正在尝试使用 Qt 在 C++ 中制作游戏。我们当前的问题是,我们需要从按钮 mousePressEvent
上的 QGraphicsTextItem
获取文本。在游戏菜单中,可以选择有多少玩家,因此我们在 for 循环中放置了一个 QGraphicsTextItem
,以便所有用户都可以输入他们的名字。由于 for 循环,我们没有每个文本项对象的名称,因此我们可以存储这些名称。我们已经设法使用 QMap
将所有内存地址存储到对象,但我们不知道如何获取其文本。我们甚至不知道这是否是最好的方法。
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
}
由于最后一行而发生的错误如下所示:
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
中获取文本?
最佳答案
在尝试开发游戏之前,您应该尝试更多地了解 C++。 (拥有公共(public)变量违反了 OO 和 C++ 范式)
但这就是您要寻找的: http://doc.qt.io/qt-5/qgraphicstextitem.html#toPlainText
编辑:
如果您无法调试某行代码,我只能建议尝试将您的代码分开,以便在一行中进行最少的调用。我没有尝试过下面的代码,但是你应该如何尝试调试你的代码:
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();
}
关于c++ - 从未命名的 QGraphicsTextItem 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37979235/
基本上,我想知道为什么文本对齐标志和 setPageSize 的组合不会以文本居中显示结束。 下面的程序几乎完全符合我的要求,只是文本最终仅水平居中。 #include #include #inc
我想使 QGraphicsTextItem 在双击时可编辑,并在我单击时使其可移动。 #include #include #include #include class TextItem: p
我必须将一些波斯语文本写入 QGraphicsTextItem,但我找不到如何让 QGraphicsTextItem 写入 RightToLeft! 任何人都可以帮助我吗? 我试过这种方式,但它对我不
我想添加一个 QGraphicsTextItem 并且我想更改背景的颜色。我的意思是我希望包含文本的 boundingRect 具有特定颜色。一种方法是创建一个 QGraphicsRectItem 并
我真的被一个与 Qt GraphicsView 相关的任务困住了。任何帮助或建议将不胜感激。 在我的 QGraphicsView 应用程序中,我添加了一些可编辑的 QGraphicsTextItems
我需要使用 创建文本项文本内容的精确宽度和高度 . 文字的高度是最重要的要求。 文本的位置应该相对于文本本身。 我还必须能够将它放置在 Canvas 上的确切位置。 假设一个(可打印的) Canvas
在我软件的“关于”框中,我使用了 QGraphicsTextItem显示关于文本。 此文本包含超文本链接(形式为:link)。 该项目正确显示(超文本链接为蓝色并带有下划线)。但是,当我点击它们时,没
如何将 QGraphicsTextItem 转换/绘制成 QPixmap? 最佳答案 您可以将它添加到 QGraphicsScene(如果它不在其中),然后使用 render() 将场景添加到 QPi
我对 C++ 有点陌生,但在确定为什么我在以下代码中遇到段错误时遇到了一些麻烦; 游戏场景.h #ifndef GAMESCENE_H #define GAMESCENE_H #include cl
我和我的一个 friend 目前正在尝试使用 Qt 在 C++ 中制作游戏。我们当前的问题是,我们需要从按钮 mousePressEvent 上的 QGraphicsTextItem 获取文本。在游戏
我需要一个对象,它结合了 QGraphicsPixmapItem 和 QGraphicsTextItem(图像 + 文本)的特性。 多重继承是最好的方法还是有任何禁忌症?如果有,您有什么建议? 最佳答
我有QGraphicsTextItem QGraphicsScene 上的对象.用户可以缩放 QGraphicsTextItem通过拖动角来移动对象。 (我正在使用自定义“转换编辑器”来执行此操作。)
是否可以阻止右键单击打开 QGraphicsTextItem 上的默认上下文菜单?带有“撤消、重做、剪切、复制、粘贴...”的菜单。在 Ubuntu 18.04 上,就是这样。我不知道这在 Windo
我有一个 QGraphicsTextItem 作为一个 lineedit,使用 setTextInteractionFlags(Qt::TextEditorInteraction); 但是,如果用户按
让这个小片段: import sys from PyQt5 import QtWidgets from PyQt5 import QtCore from PyQt5 import QtGui from
我有一个包含多个 (QGraphicsTextItem) 的场景,我需要控制它们的颜色,那么如何更改 QGraphicsTextItem 的颜色?有可能吗?到目前为止,我已经尝试了 3 天。请帮忙 提
我想要一个 python 应用程序,它显示一堆小肖像和下面的名字。像那样: 它们应该是可移动和可编辑的(通过双击文本)。 我正在使用 PyQt4,所以我发现,为 Canvas 使用 QGraphics
我正在 Windows XP 上使用 QGraphicsScene 开发 Qt 4.8 应用程序。当用户双击 QGraphicsTextItem 时,我调用 textItem->setTextInte
我正在为一个添加了框架的 QGraphicsTextItem 制作动画。在动画期间,文本似乎在框架内轻微晃动,这非常烦人。 示例代码: class MovingFrameText : public Q
如果我想在 QGraphicsView 中放置一个字母(QGraphicsTextItem),有什么方法可以将文本设置为显示在我指定的坐标中间?如果我使用 QGraphicsTextItem.setP
我是一名优秀的程序员,十分优秀!