Hi
I subclass QGraphicsWidget and override boundingRect and paint methods.
Here is what I did shortly
CustomItem :: CustomItem()
{
pen
= QPen(QBrush(Qt
::gray),
5,Qt
::SolidLine,Qt
::RoundCap,Qt
::BevelJoin);
}
CustomItem :: ~CustomItem()
{
}
QRectF CustomItem
::boundingRect() const {
}
void CustomItem
::paint(QPainter *painter,...
) {
painter->setPen(pen);
painter->drawLine(0,0,100,0);
}
CustomItem :: CustomItem()
{
pen = QPen(QBrush(Qt::gray),5,Qt::SolidLine,Qt::RoundCap,Qt::BevelJoin);
}
CustomItem :: ~CustomItem()
{
}
QRectF CustomItem::boundingRect() const
{
return QRectF(0,0,102,5);
}
void CustomItem::paint(QPainter *painter,...)
{
painter->setPen(pen);
painter->drawLine(0,0,100,0);
}
To copy to clipboard, switch view to plain text mode
And I added my CustomItem(s) to QGraphicsGridLayout
For this I used
QGraphicsGridLayout *grid = new QGraphicsGridLayout;
CustomItem *custom1 = new CustomItem;
CustomItem *custom2 = new CustomItem;
grid->addItem(custom1,0,1);
grid->addItem(custom2,0,2);
QGraphicsWidget *container = new QGraphicsWidget;
container->setLayout(grid);
scene->addItem(container);
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsGridLayout *grid = new QGraphicsGridLayout;
CustomItem *custom1 = new CustomItem;
CustomItem *custom2 = new CustomItem;
grid->addItem(custom1,0,1);
grid->addItem(custom2,0,2);
QGraphicsWidget *container = new QGraphicsWidget;
container->setLayout(grid);
scene->addItem(container);
To copy to clipboard, switch view to plain text mode
When I do this
Two custom items had painted on each other(I guess because of paint draws them on same coordinates)
But I want them to paint one after another, I assume grid layout does it for me but it does not
How can I overcome this?
Note: I use QGraphicsWidget class instead of QGraphicsItem or QGraphicsLineItem because I think I may easily
create my user interface without supplying coordinates for each item.
Bookmarks