I have a class
{
public:
Letter();
~Letter();
protected:
private:
};
class Letter : public QGraphicsTextItem
{
public:
Letter();
~Letter();
void setText(const QString &text);
QString getText();
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect() const;
QPainterPath shape() const;
private:
QString myText;
QTextOption myTextOption;
};
To copy to clipboard, switch view to plain text mode
Letter::Letter()
{
myTextOption.setAlignment(Qt::AlignCenter);
myTextOption.
setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
}
Letter::~Letter() {
}
QRectF Letter
::boundingRect() const { }
path.addRect(boundingRect());
return path;
}
painter->fillRect(boundingRect(),Qt::red);
painter->setPen(Qt::white);
painter
->setFont
(QFont("Open Sans",
60));
painter->drawText(boundingRect(),myText,myTextOption);
}
void Letter
::setText(const QString &text
) { myText = text;
this->update();
}
return myText;
}
Letter::Letter()
{
myTextOption.setAlignment(Qt::AlignCenter);
myTextOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
}
Letter::~Letter() {
}
QRectF Letter::boundingRect() const {
return QRectF(0,0,145,145);
}
QPainterPath Letter::shape() const {
QPainterPath path;
path.addRect(boundingRect());
return path;
}
void Letter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
painter->fillRect(boundingRect(),Qt::red);
painter->setPen(Qt::white);
painter->setFont(QFont("Open Sans", 60));
painter->drawText(boundingRect(),myText,myTextOption);
QGraphicsTextItem::paint(painter,option,widget);
}
void Letter::setText(const QString &text) {
myText = text;
this->update();
}
QString Letter::getText() {
return myText;
}
To copy to clipboard, switch view to plain text mode
I add letters to QGraphicsScene
{
public:
GameField();
};
GameField::GameField()
{
this
->setBackgroundBrush
(QBrush(QColor(230,
230,
230),Qt
::SolidPattern));
for (int i = 10; i < 610; i += 150) {
for (int j = 10; j < 610; j += 150) {
Letter *letter = new Letter();
letter
->setText
(QString('A'+qrand
()%26
));
this->addItem(letter);
letter->setPos(i, j);
}
}
}
class GameField : public QGraphicsScene
{
public:
GameField();
};
GameField::GameField()
{
this->setBackgroundBrush(QBrush(QColor(230,230,230),Qt::SolidPattern));
for (int i = 10; i < 610; i += 150) {
for (int j = 10; j < 610; j += 150) {
Letter *letter = new Letter();
letter->setText(QString('A'+qrand()%26));
this->addItem(letter);
letter->setPos(i, j);
}
}
}
To copy to clipboard, switch view to plain text mode
but when I try to click on any letter, my program crashes.
Bookmarks