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
{
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;
}
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)
{
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;
}
To copy to clipboard, switch view to plain text mode
------------------------------
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);
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);
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);
To copy to clipboard, switch view to plain text mode
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!
Bookmarks