PDA

View Full Version : Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem



leinad
2nd November 2021, 18:36
Hi,

I created a custom QGraphicsRectItem to allow me to handle mouse events which works fine. The issue I'm having is when I try to set the font of a QGraphicsTextItem which happens to be in my custom QGraphicsRectItem, it won't change I can't even change the text width. Below is a sample code.


CustomRect:
CustomRectItem::CustomRectItem()
{

}

//subclass QGraphicsRectItem to allow mouse events
void CustomRectItem::mousePressEvent(QGraphicsSceneMous eEvent *event)
{
int xyCellSize = 20;

if(event->isAccepted())
{
if(event->button() == Qt::LeftButton)
{

int x = ((event->scenePos().x() - sceneStartCornerX_) / xyCellSize) + 2; //add one to accomodate the cell for the pingee and pinger text - 32
int y = ((event->scenePos().y() - sceneStartCornerY_) / xyCellSize) + 2; // 217
Q_EMIT sendRectClicked(y, x);
}
}
}
void CustomRectItem::getViewCoordinates(int x, int y)
{
sceneStartCornerX_ = x;
sceneStartCornerY_ = y;
}

------------------------------

In my main to change the fonts size:

CustomRectItem *rect = new CustomRectItem;
rect->setRect(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement), XY_CELL_SIZE, XY_CELL_SIZE);
QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
QFont times("Times", 10);
appIDText->setFont(times);
appIDText->setPlainText(QString::number(appID));
scene->addItem(rect);

The text is in the rect just can't change the font size. Can someone please explain to me what I'm doing wrong?

Thanks!

leinad
3rd November 2021, 13:22
Hi,

I created a custom QGraphicsRectItem to allow me to handle mouse events which works fine. The issue I'm having is when I try to set the font of a QGraphicsTextItem which happens to be in my custom QGraphicsRectItem, it won't change I can't even change the text width. Below is a sample code.

CustomRect:
CustomRectItem::CustomRectItem()
{

}

//subclass QGraphicsRectItem to allow mouse events
void CustomRectItem::mousePressEvent(QGraphicsSceneMous eEvent *event)
{
int xyCellSize = 20;

if(event->isAccepted())
{
if(event->button() == Qt::LeftButton)
{

int x = ((event->scenePos().x() - sceneStartCornerX_) / xyCellSize) + 2; //add one to accomodate the cell for the pingee and pinger text - 32
int y = ((event->scenePos().y() - sceneStartCornerY_) / xyCellSize) + 2; // 217
Q_EMIT sendRectClicked(y, x);
}
}
}
void CustomRectItem::getViewCoordinates(int x, int y)
{
sceneStartCornerX_ = x;
sceneStartCornerY_ = y;
}

------------------------------

In my main to change the fonts size:
CustomRectItem *rect = new CustomRectItem;
rect->setRect(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement), XY_CELL_SIZE, XY_CELL_SIZE);
QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
QFont times("Times", 10);
appIDText->setFont(times);
appIDText->setPlainText(QString::number(appID));
scene->addItem(rect);

The text is in the rect just can't change the font size. Can someone please explain to me what I'm doing wrong?

Thanks!

No one has any ideas?

d_stranz
3rd November 2021, 15:02
Have you set the QGraphicsItem::ItemIgnoresTransformations flag on your QGraphicsTextItem? This will prevent the QGraphicsRectItem from scaling the text according to its own scaling transformations (which are applied when the scene is fit into the view).


No one has any ideas?

You should be a little more patient. This forum has been slow lately, and not everyone reads it every day.

leinad
3rd November 2021, 15:20
Have you set the QGraphicsItem::ItemIgnoresTransformations flag on your QGraphicsTextItem? This will prevent the QGraphicsRectItem from scaling the text according to its own scaling transformations (which are applied when the scene is fit into the view).



You should be a little more patient. This forum has been slow lately, and not everyone reads it every day.

My apologies. I'll take a look at what you mentioned. Thanks.