Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem
-
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(QGraphicsSceneMouseEvent *event)
{
int xyCellSize = 20;if(event->isAccepted()) { if(event->button() == Qt::LeftButton) { //convert scenePos to row/col values // qDebug() << event->scenePos().x() << event->scenePos().y(); 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 // qDebug() << "row = " << y << "col = " << x; 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!
-
Hi,
What if you create an item with its text content rather than its size ?
-
@leinad said in Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem:
QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
Pass the text rather than a text here.
-
If you mean:
QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
QFont times("Times", 10);
times.setPointSize(2);
appIDText->setFont(times);I get the same results :(
-
Do you have the same issue if you just add a QGraphicsTextItem to your scene ?
-
Yes, I tried this and same results
// QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
QFont times("Times", 10);
times.setPointSize(2);
appIDText->setFont(times);
// appIDText->setPlainText(QString::number(appID));
scene->addItem(appIDText); -
Why are you setting the point size to two after creating your QFont object with a size of 10 ?
-
Yes, I tried this and same results
// QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
QFont times("Times", 10);
times.setPointSize(2);
appIDText->setFont(times);
// appIDText->setPlainText(QString::number(appID));
scene->addItem(appIDText);