2010-05-03 66 views
0

我該如何子類化QGraphicsLayoutItem? 我寫了一個繼承QGraphicsLayoutItem類並重新實現sizeHint和setGeometry的類,但是當我將自定義項添加到線性或網格佈局時。它沒有顯示? 缺什麼?子類化QGraphicsLayoutItem

這是我的試用版。

//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); 
... 
+1

Qt的文檔建議重新實現updateGeometry方法。如果這沒有幫助,你能否提供我們的代碼樣本? – Lohrun 2010-05-04 09:49:32

+0

@onurozcelik:如果你在這裏發佈你的示例代碼,這將會更好,這是行不通的。 – mosg 2010-05-04 11:25:52

+0

嗨,請原諒我,但在評論中添加代碼並不是一個好的決定,因爲它根本無法讀取。最好編輯你的第一篇文章(你的第一個問題)並在那裏添加代碼。 – mosg 2010-05-06 05:22:06

回答

0

可能會是有用的,從我工作的項目只是一個小例子:

GraphicsLayoutItem::GraphicsLayoutItem(QGraphicsItem *parent) : 
QGraphicsItemGroup(parent), 
QGraphicsLayoutItem(), 
{ 

addToGroup(item_); 
addToGroup(text_item_); 

setGraphicsItem(this); 
} 

GraphicsLayoutItem::~GraphicsLayoutItem() 
{ 
removeFromGroup(item_); 
removeFromGroup(text_item_); 

delete item_; 
delete text_item_; 

setGraphicsItem(0); 
} 

void GraphicsLayoutItem::setGeometry(const QRectF &r) 
{ 
prepareGeometryChange(); 
QGraphicsLayoutItem::setGeometry(r); 
setPos(r.topLeft()); 
} 

QRectF GraphicsLayoutItem::boundingRect() const 
{ 
return QRectF(QPointF(0, 0), geometry().size()); 
} 

QSizeF GraphicsLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) 
const 
{ 

switch (which) 
{ 
    case Qt::MinimumSize: 
    case Qt::PreferredSize: 
     return this->boundingRect().size(); 

    case Qt::MaximumSize: 
     return QSizeF(parentItem()->boundingRect().width(), 
       MyOptions::DefaultHeight); 

    default: 
     return this->boundingRect().size(); 
} 

return constraint; 
}