QGraphicsLayoutItemをどのようにサブクラス化できますか? QGraphicsLayoutItem をサブクラス化し、sizeHint と setGeometry を再実装するクラスを作成しますが、カスタム アイテムを線形レイアウトまたはグリッド レイアウトに追加すると、表示されませんか?何が欠けている?
これが私の試練です。
//basicitem.h
#include <QGraphicsWidget>
#include <QtCore/QRectF>
#include <QtGui/QPainter>
#include <QtGui/QBrush>
#include <QtGui/QPen>
#include <QtCore/QSizeF>
class BasicItem : public QGraphicsWidget
{
public:
BasicItem(qreal x1,qreal y1,qreal x2,qreal y2);
~BasicItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
private:
qreal x1,y1,x2,y2;
};
//basicitem.cpp
#include "basicitem.h"
#include <math.h>
BasicItem::BasicItem(qreal x1, qreal y1, qreal x2, qreal y2)
{
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
}
BasicItem::~BasicItem()
{
}
QRectF BasicItem::boundingRect() const
{
return QRectF(x1,y1,abs(x2-x1),5);
}
void BasicItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QPen(QBrush(Qt::yellow),4,Qt::SolidLine,Qt::FlatCap,Qt::BevelJoin));
painter->drawLine(x1,y1,x2,y2);
}
QSizeF BasicItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(which);
Q_UNUSED(constraint);
return QSizeF(abs(x2-x1),5);
}
//compositeitem.h
#include <QGraphicsItemGroup>
#include <QGraphicsLayoutItem>
#include <QSizeF>
#include <QList>
#include "basicitem.h"
class CompositeItem:public QGraphicsItemGroup,public QGraphicsLayoutItem
{
public: CompositeItem(QList<BasicItem *> children);
~CompositeItem();
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
void setGeometry(const QRectF &rect);
void updateGeometry();
};
//compositeitem.cpp
#include "compositeitem.h"
CompositeItem::CompositeItem(QList<BasicItem *> children)
{
BasicItem *child;
foreach(child,children)
{
addToGroup(child);
}
}
CompositeItem::~CompositeItem()
{
}
QSizeF CompositeItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_UNUSED(which);
Q_UNUSED(constraint);
//Arbitrary values but big enough
return QSizeF(300,300);
}
void CompositeItem::setGeometry(const QRectF &rect)
{
Q_UNUSED(rect);
QGraphicsLayoutItem::setGeometry(QRectF(0,0,300,300));
}
void CompositeItem::updateGeometry()
{
QGraphicsLayoutItem::updateGeometry();
}
//mainwindow.cpp - important parts
...
QGraphicsScene *scene = new QGraphicsScene;
ui->graphicsView->setScene(scene);
BasicItem *basic1 = new BasicItem(10,400,110,400);
BasicItem *basic2 = new BasicItem(10,400,110,500);
QList<BasicItem *> basicItemList;
basicItemList.push_back(basic1);
basicItemList.push_back(basic2);
CompositeItem *ci = new CompositeItem(basicItemList);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
layout->addItem(ci);
QGraphicsWidget *container = new QGraphicsWidget;
container->setLayout(layout);
scene->addItem(container);
...