Q_OBJECT;
public:
private:
void adjustRect();
QList<QString> fields;
};
{
}
{
painter->setPen(Qt::black);
painter
->setBrush
(QColor(230,
230,
230));
painter->drawRect(rect());
painter->drawText(pos, name);
qDebug() << rect().width();
painter->drawLine(pos.x() - 10, pos.y() + 5, pos.x() + rect().width() - 10, pos.y()+5);
foreach(str, fields) {
pos.setY(pos.y() + 20);
painter->drawText(pos, str);
}
}
{
this->name = name;
adjustRect();
}
void Entity
::addField(QString field
) {
fields.append(field);
adjustRect();
}
void Entity::adjustRect()
{
qreal maxx = 20;
maxx = (maxx < name.length()*10 ? name.length()*10 : maxx);
foreach(str, fields) {
maxx = (maxx < str.length()*10 ? str.length()*10 : maxx);
}
setRect(rect().x(), rect().y(), maxx, fields.count()*30 + 10);
}
class Entity : public QObject, public QGraphicsRectItem {
Q_OBJECT;
public:
Entity(QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0);
void setName(QString name);
void addField(QString field);
private:
void adjustRect();
QString name;
QList<QString> fields;
};
Entity::Entity(QGraphicsItem *parent)
: QGraphicsRectItem(parent)
{
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
}
void Entity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(Qt::black);
painter->setBrush(QColor(230,230,230));
painter->drawRect(rect());
QPointF pos = QPointF(rect().x() + 10, rect().y() + 20);
painter->drawText(pos, name);
qDebug() << rect().width();
painter->drawLine(pos.x() - 10, pos.y() + 5, pos.x() + rect().width() - 10, pos.y()+5);
QString str;
foreach(str, fields) {
pos.setY(pos.y() + 20);
painter->drawText(pos, str);
}
}
void Entity::setName(QString name)
{
this->name = name;
adjustRect();
}
void Entity::addField(QString field)
{
fields.append(field);
adjustRect();
}
void Entity::adjustRect()
{
qreal maxx = 20;
maxx = (maxx < name.length()*10 ? name.length()*10 : maxx);
QString str;
foreach(str, fields) {
maxx = (maxx < str.length()*10 ? str.length()*10 : maxx);
}
setRect(rect().x(), rect().y(), maxx, fields.count()*30 + 10);
}
To copy to clipboard, switch view to plain text mode
Bookmarks