Подскажите
создаю объект
от класса КМТ
C++ (Qt)
class Text: public QGraphicsObject
{
Q_OBJECT
public:
explicit Text(int w, int h, QString str, QGraphicsObject*parent = 0);
virtual ~Text();
QPoint calculatePosition(int , int);
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
int boundingWidth;
int boundingHeight;
QGraphicsSimpleTextItem*text;
};
Text::Text(int w, int h, QString str, QGraphicsObject *parent)
: QGraphicsObject()
{
qDebug()<<"TEXT";
boundingWidth = w;
boundingHeight = h;
text = new QGraphicsSimpleTextItem(this);
QFont font;
int size = textFont;
font.setPixelSize(size);
text->setFont(font);
QFontMetrics fontmetrics(font);
int width = fontmetrics.width(str);
int height = fontmetrics.height();
while((size > 0) && ((w < width) || (h < height)))
{
size--;
font.setPixelSize(size);
QFontMetrics fontmetrics(font);
width = fontmetrics.width(str);
height = fontmetrics.height();
}
QPoint pos = calculatePosition(width,height);
text->setFont(font);
text->setText(str);
text->setPos(pos.x(),pos.y());
}
Text::~Text()
{
}
QPoint Text::calculatePosition(int textWidth, int textHeight)
{
int tmp;
QPoint pos;
tmp = boundingWidth - textWidth;
if(tmp >= 0)
{
pos.setX(tmp/2);
}
tmp = boundingHeight - textHeight;
if(tmp >= 0)
{
pos.setY(tmp/2);
}
return pos;
}
QRectF Text::boundingRect() const
{
return QRectF(0,0,boundingWidth,boundingHeight);
}
QPainterPath Text::shape() const
{
QPainterPath path;
QPolygon polygon;
polygon << QPoint(0 ,0)
<< QPoint(boundingWidth,0)
<< QPoint(boundingWidth,boundingHeight)
<< QPoint(0,boundingHeight)
<< QPoint(0,0);
path.addPolygon(polygon);
return path;
}
void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen(QBrush(Qt::black),1,Qt::SolidLine);
painter->setBrush(Qt::white);
painter->setPen(pen);
painter->drawPath(shape());
}
class KMT: public QGraphicsObject
{
Q_OBJECT
public:
explicit KMT(float indata, int range,QGraphicsItem *parent = 0);
virtual ~KMT();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPainterPath shape() const;
private:
Text *numberRange;
int boundingWidth;
int boundingHeight;
};
KMT::KMT(float indata,int range, QGraphicsItem *parent):
QGraphicsObject()
{
qDebug()<<"Create KMT";
qDebug()<<"WIDTH * l_range = "<<WIDTH*l_range;
qDebug()<<"HEIGHT * h_header * h_number_range = "<<HEIGHT*h_header*h_number_range;
numberRange = new Text(30,20,"h",this);
numberRange->setPos(10,10);
boundingWidth = WIDTH;
boundingHeight = HEIGHT;
}
KMT::~KMT()
{
qDebug()<<"delete KMT";
}
QRectF KMT::boundingRect() const
{
return QRectF(0,0,200,100);
}
void KMT::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen(QBrush(Qt::black),1,Qt::SolidLine);
painter->setBrush(Qt::white);
painter->setPen(pen);
painter->drawPath(shape());
}
QPainterPath KMT::shape() const
{
QPainterPath path;
QPolygon polygon;
polygon << QPoint(0 ,0)
<< QPoint(boundingWidth,0)
<< QPoint(boundingWidth,boundingHeight)
<< QPoint(0,boundingHeight)
<< QPoint(0,0);
path.addPolygon(polygon);
return path;
}
SceneKMT::SceneKMT(QObject *parent):
QGraphicsScene()
{
setBackgroundBrush(QBrush(Qt::lightGray));
setSceneRect(0,0,800,600);
kmt = new KMT(0.01,3);
kmt->setPos(0,400);
addItem(kmt);
}
и добавляю на сцену
в объекте КМТ находится объет типа класса Text
но почему-то на сцене он не появляется
А если объект класса Text просто на сцену добавить, то все норм.